目前我使用以下方法将属性注入bean:
app.properties:
SingletonBean.valueA=this is a value
spring.xml:
<context:property-placeholder location="classpath:app.properties"/>
<context:component-scan base-package="..."/>
SingletonBean.java:
@Component
public class SingletonBean {
@Value("${SingletonBean.valueA}")
private String valueA;
}
这非常有用,并且非常方便能够将我的所有配置保存在一个简单的属性文件中。有什么方法可以扩展它与同一类的多个Beans一起工作?我需要执行以下操作,2个bean具有不同的属性:
@Autowired private SampleBean beanA;
@Autowired private SampleBean beanB;
我知道我可以使用@Qualifier(name=...)
注释来支持以下xml:
<bean id="beanA" class="SampleBean">
<property name="key1" value="A1"/>
<property name="key2" value="A2"/>
</bean>
<bean id="beanB" class="SampleBean">
<property name="key1" value="B1"/>
<property name="key2" value="B2"/>
</bean>
但是有了这个,我被迫在SampleBean
课程中使用旧式设置器,我更喜欢使用较新的@Value
注释。
任何人都知道如何实现我想要的东西,这与我目前注入其他豆类的方式最为一致吗?
简单的解决方案就是将所有属性注入到使用2 SampleBean
个实例的bean中,并使用new
创建它们。然而,在我的真实代码中,实际上有3个实例,每个实例有15个左右的属性。这比我想要的更加残酷和重复。