我有一个Spring Java Configuration类,它定义了一个bean,例如:
@Configuration
public class SomeConfig{
private String someProperty;
public SomeConfig(String someProperty){
this.someProperty=someProperty;
}
@Bean
public SomeBean someBean(){
SomeBean s = new SomeBean(someProperty);
return s;
}
}
我需要有几个SomeBean
个实例,每个实例都配置了不同的someProperty
值。
@Import
同一个类吗?
SELF-ANSWERED :如果您导入相同的@Configuration类,它将覆盖现有的类。更新
在XML中,我可以这样做:
<bean class="x.y.z.SomeBean">
<constructor-arg value="1"/>
</bean>
<bean class="x.y.z.SomeBean">
<constructor-arg value="2"/>
</bean>
我正在寻找与Java Config相当的
答案 0 :(得分:2)
我只需要使用另一个@Configuration类,根据需要定义多个SomeConfig bean:
@Configuration
public class ApplicationConfig{
@Bean
public SomeConfig someConfig1(){
return new SomeConfig("1");
}
@Bean
public SomeConfig someConfig2(){
return new SomeConfig("2");
}
}
答案 1 :(得分:0)
@Configuration
public class SomeConfig{
private String someProperty;
@Bean
public OtherBean otherBeanOne(){
OtherBean otherBean = new OhterBean();
otherBean.setSomeBean(someBean("property1"));
return otherBean;
}
@Bean
public OtherBean otherBeanTwo(){
OtherBean otherBean = new OhterBean();
otherBean.setSomeBean(someBean("property2"));
return otherBean;
}
@Bean
public SomeBean someBean(String someProperty){
SomeBean s = new SomeBean(someProperty);
return s;
}
}