模板中的java Spring IOC嵌套bean重写属性值
我有一个template bean
,它有一个嵌套的bean
。嵌套bean有两个重要属性,一个对其他3个bean定义有效,但它们有一个secong属性,每个bean中都会发生变化
我的模板看起来像这样。没有类的bean
<bean id="myBeanTemplate" abstract="true" scope="prototype">
<property name="school">
<bean class="com.model.School" scope="prototype">
<property name="status" value="true"/><!--is all the same for all the child beans..->
/*address=?? the property which is change across the children beans.. the property to be set*/
</bean>
</property>
</bean>
这里我不设置addres
属性,因为它们在下面的bean声明中有所不同,我只想在上面设置bean模板和override the address property only
。就这样
<bean id="myBeanForStudentsInSchool13" class="com.model.Students" parent="myBeanTemplate" scope="prototype">
here i want to set the address property to a value
</bean>
<bean id="myBeanForStudentsInSchool23" class="com.model.Students" parent="myBeanTemplate" scope="prototype">
here i want to set the address property a different value
</bean>
但就像是一个嵌套的bean,我不知道如何引用它
更新
我只允许使用声明性配置
非常感谢
# 1 楼答案
使用Java配置检查此解决方案
学校模式:
Bean模板:
学生班级:
弹簧配置:
编辑
对于XML配置,请检查此示例(注意子bean中的点):
您还可以在此处查看更详细的答案:spring - constructor injection and overriding parent definition of nested bean