Spring:带有@Autowired(required = false) 参数的构造函数的

我有一个服务类,我想用构造函数参数的不同传入值动态初始化它:


@Service

public class SomeServiceImpl implements SomeService {


    private final SomeProperties someProperties;

    private final String url;

    private final String password;


    private final Logger log = LoggerFactory.getLogger(SomeServiceImpl.class);


    @Autowired

    public SomeServiceImpl(SomeProperties someProperties,

                             @Autowired(required = false) String url,

                             @Autowired(required = false) String password) {

        this.someProperties = someProperties;

        this.url = url;

        this.password = password;

    }

是否可以在运行时@Service使用自己提供的@Autowired(required = false)参数(在本例中为自己的 url 和密码)在另一个 spring 组件类中对其进行初始化?这段代码会是什么样子?


慕尼黑的夜晚无繁华
浏览 473回答 2
2回答

隔江千里

你可以这样做@Configurationclass SomeConfigClass {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; SomeProperties someProperties&nbsp; &nbsp; @Value("${url1}")&nbsp; &nbsp; String url1&nbsp; &nbsp; @Value("${password1}")&nbsp; &nbsp; String password1&nbsp; &nbsp; ..............&nbsp; &nbsp; &nbsp;// Do this for other url's and properties or check out @ConfigurationProperties&nbsp; &nbsp; ..............&nbsp; &nbsp; @Bean("someService1")&nbsp; &nbsp; public SomeService() {&nbsp; &nbsp; &nbsp; &nbsp; return new SomeService(someProperties, url1, password1);&nbsp; &nbsp; }&nbsp; &nbsp; @Bean("someService2")&nbsp; &nbsp; public SomeService() {&nbsp; &nbsp; &nbsp; &nbsp; return new SomeService(someProperties, url2, password2);&nbsp; &nbsp; }&nbsp; &nbsp; ...............&nbsp; &nbsp; ..............}创建工厂类@Configuration //typo correctedclass SomeServiceFactory {&nbsp; @Autowired // Spring will Autowire all instances of SomeService with bean name as key&nbsp; Map<String, SomeService> someServiceMap;&nbsp; public SomeService getSomeServiceByName(String name) {&nbsp; &nbsp; return someServiceMap.get(name);&nbsp; }}然后你可以像这样使用实例@RestControllerclass SomeController {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; SomeServiceFactory someServiceFactory;&nbsp; &nbsp; public void someEndpoint() {&nbsp; &nbsp; &nbsp;SomeService someService1 = SomeServiceFactory.getSomeServiceByName("someService1"); //You need to decide what argument to pass based on condition&nbsp; &nbsp; &nbsp;someService1.someFunction(...); // this will have url1 and password1&nbsp; &nbsp;}&nbsp;}

收到一只叮咚

用户名和密码从何而来?也许您可以简单地从构造函数中删除它们并使用 @Value 注释从属性文件中读取值?@Servicepublic class SomeServiceImpl implements SomeService {&nbsp; &nbsp; private final SomeProperties someProperties;&nbsp; &nbsp; @Value("${service.url}")&nbsp; &nbsp; private String url;&nbsp; &nbsp; @Value("${service.password}")&nbsp; &nbsp; private String password;&nbsp; &nbsp; private final Logger log = LoggerFactory.getLogger(SomeServiceImpl.class);&nbsp; &nbsp; @Autowired&nbsp; &nbsp; public SomeServiceImpl(SomeProperties someProperties) {&nbsp; &nbsp; &nbsp; &nbsp; this.someProperties = someProperties;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java