【问题标题】:Spring IOC nested bean in template override property value模板中的 Spring IOC 嵌套 bean 覆盖属性值
【发布时间】:2014-08-01 19:39:51
【问题描述】:

我有一个template bean,其中有一个嵌套的bean。嵌套 bean 有 2 个重要属性,一个对其他 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>

但是 like 是一个嵌套的 bean,我不知道如何引用它...

更新

我可以只使用声明性配置...

非常感谢..

【问题讨论】:

  • 您无法访问嵌套 bean。
  • 为什么不公开(非嵌套)和原型嵌套bean?。 “也许”(我不确定)是抽象的,它让孩子覆盖它

标签: java spring inversion-of-control


【解决方案1】:

使用 Java Config 检查此解决方案。

学校模型:

public class School {

    private boolean status;
    private String address;

    // getters & setters

}

豆模板:

public abstract class MyBeanTemplate {

    private School school;

    public School getSchool() {
        return school;
    }

    public void setSchool(School school) {
        this.school = school;
    }

}

学生班:

public class Students extends School {

}

弹簧配置:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {

    @Bean
    public Students myBeanForStudentsInSchool13() {
        Students students = new Students();
        students.setAddress("myBeanForStudentsInSchool13");

        return students;
    }

    @Bean
    public Students myBeanForStudentsInSchool23() {
        Students students = new Students();
        students.setAddress("myBeanForStudentsInSchool23");

        return students;
    }
}

编辑

对于 XML 配置检查这个例子(注意子 bean 中的点):

<bean id="myBeanTemplate" abstract="true" class="com.beans.MyBeanTemplate">
    <property name="school">
        <bean class="com.model.School">
            <property name="status" value="true"/>
        </bean>
    </property>
</bean>

<bean id="myBeanForStudentsInSchool13" class="com.model.Students" parent="myBeanTemplate">    
    <property name="school.address" value="myBeanForStudentsInSchool13"/>
</bean>

<bean id="myBeanForStudentsInSchool23" class="com.model.Students" parent="myBeanTemplate">    
    <property name="school.address" value="myBeanForStudentsInSchool23"/>
</bean>

你也可以在这里查看更详细的答案:spring - constructor injection and overriding parent definition of nested bean

【讨论】:

  • 我只允许使用声明性配置。
猜你喜欢
  • 2017-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多