我有一个包含同一组件的两个 Autowiring 实例的 bean:
@Component
public SomeBean {
@Autowired
private SomeOtherBean someOtherBean1;
@Autowired
private SomeOtherBean someOtherBean2;
...
}
SomeOtherBean 有一个原型(prototype)作用域:
@Component
@Scope("prototype")
public SomeOtherBean {
@Value("...")
private String configurable;
}
每个 Autowiring 的 SomeOtherBean 的可配置值需要不同,并将通过属性占位符提供:
configurable.1=foo
configurable.2=bar
理想情况下,我想使用注释来指定可配置属性的值。
通过 XML 执行此操作很容易,但我想知道这是否是
- a) 无法使用注释或
- b) 如何做到。
最佳答案
也许这与您的想法略有不同,但您可以使用基于 @Configuration
的方法轻松完成,例如:
@Configuration
public class Config {
@Bean
@Scope("prototype")
public SomeOtherBean someOtherBean1(@Value("${configurable.1}") String value) {
SomeOtherBean bean = new SomeOtherBean();
bean.setConfigurable(value);
return bean;
}
@Bean
@Scope("prototype")
public SomeOtherBean someOtherBean2(@Value("${configurable.2}") String value) {
// etc...
}
}
关于java - 如何使用注释在 @Scope ("prototype") bean 中指定实例特定的 @Value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19004489/