Ben*_*ner 5 java spring spring-annotations
我有一个bean包含两个相同组件的自动装配实例:
@Component
public SomeBean {
@Autowired
private SomeOtherBean someOtherBean1;
@Autowired
private SomeOtherBean someOtherBean2;
...
}
Run Code Online (Sandbox Code Playgroud)
SomeOtherBean有一个原型范围:
@Component
@Scope("prototype")
public SomeOtherBean {
@Value("...")
private String configurable;
}
Run Code Online (Sandbox Code Playgroud)
每个自动装配的SomeOtherBean的可配置值需要不同,并且将通过属性占位符提供:
configurable.1=foo
configurable.2=bar
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想使用注释来指定可配置属性的值.
通过XML执行此操作很容易,但我想知道是否这样
也许这与您的想法略有不同,但您可以使用@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...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
504 次 |
| 最近记录: |