Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring IOC重写bean的属性_Java_Spring_Inversion Of Control - Fatal编程技术网

Java Spring IOC重写bean的属性

Java Spring IOC重写bean的属性,java,spring,inversion-of-control,Java,Spring,Inversion Of Control,我需要3个bean,每个bean有3个属性,其中2个是其他bean的相关属性值,但它们只有1个属性不同。我有这样的想法: 这段代码非常冗余。如果我想定义一个bean,并且某种类型只覆盖属性typeOf? 更新 status和code的值是在其他bean中使用Spring语言定义的,这在某种程度上是一个示例。是的,只是在类定义中执行以下操作: ... private boolean status = true; private int code = 13; private int typeOf

我需要3个bean,每个bean有3个属性,其中2个是其他bean的相关属性
值,但它们只有1个属性不同。我有这样的想法:


这段代码非常冗余。如果我想定义一个
bean
,并且某种类型只覆盖属性
typeOf
更新


status和code的值是在其他bean中使用
Spring语言定义的,这在某种程度上是一个示例。

是的,只是在类定义中执行以下操作:

...
private boolean status = true;
private int code = 13;
private int typeOf;
...
然后,Spring配置将如下所示:

<bean id="a" class="myClassPath" scope="prototype">
   <property name='typeOf' value='1'/>
</bean>

例如,使用
autowired=“byName”
,创建具有
状态和
code
名称的bean:

<bean id="status" class="java.lang.Boolean">
    <constructor-arg value="true"/>
</bean>

<bean id="code" class="java.lang.Integer">
    <constructor-arg value="13"/>
</bean>

<bean id="a" class="myClassPath" autowired="byName" scope="prototype">
    <property name='typeOf' value='1'/>
</bean>

<bean id="b" class="myClassPath" autowired="byName" scope="prototype">
    <property name='typeOf' value='2'/>
</bean>

<bean id="c" class="myClassPath" autowired="byName" scope="prototype">
    <property name='typeOf' value='3'/>
</bean>

在这样的场景中利用bean模板继承:

    <bean id="yourCommonProperties"  abstract="true" scope="prototype">
        <property name="status" value='#{otherBean.myMethod()}'/>
        <property name="code" value='#{otherBean.myOtherMethod()}'/>
    </bean>

这里我们不是指定类,而是共享公共属性,并在每个bean定义中使用parent属性将其引用为:

    <bean id="a" class="myClassPath" parent="yourCommonProperties" scope="prototype">
        <property name='typeOf' value='1'/>
    </bean>
    <bean id="b" class="myClassPath" parent="yourCommonProperties" scope="prototype">
        <property name='typeOf' value='2'/>
    </bean>
    <bean id="c" class="myClassPath" parent="yourCommonProperties" scope="prototype">
        <property name='typeOf' value='3'/>
    </bean>


使用带有
@PostConstruct
注释的构造函数或方法,而不是初始化声明中的字段。@LuiggiMendoza请给出原因,而不是说:那样使用,因为。这可能很棘手。我不是说它不起作用,只是想象一下下面的情况。myClassPath有
私有java.lang.Integer代码以这种方式使用它,三个实例将共享完全相同的内存块,因此如果其中一个实例修改它,其他实例的值也将发生更改。正如我所说,这可能很棘手。有时我们需要这个功能,但更多时候我们不需要。@abc不,它不是,因为Integer类是不可变的。不能更改整数实例的值。但如果一个变量指向一个整数实例,则可以使用其他值使其指向另一个实例。而且不会影响其他课程。您所说的就像是说,如果两个类有
String textField=“someText”
如果更改其中一个类中
textField
的值,则第二个类也会受到影响。这不是真的,因为String类也是不可变的,就像Integer一样。