【问题标题】:How to set bean property value from other bean property value如何从其他 bean 属性值设置 bean 属性值
【发布时间】:2011-06-03 22:57:40
【问题描述】:

例如,我们有带有字符串属性 propertyA 的 bean beanA

<bean name="beanA" class="...">
     <property name="propertyA"><value>some string value </value></property>
</bean>

第二个bean beanB也有字符串属性propertyB,我想用与beanA.propertyA相同的值初始化这个属性,我想我需要做这样的事情:

<bean name="beanB" class="...">
     <property name="propertyB"><value>beanA.propertyA</value></property>
</bean>

但这不起作用,表达式 beanA.propertyA 被威胁为字符串值。

这可能违反了 IoC 理论,并且不受 Spring 支持。

谢谢。

【问题讨论】:

    标签: java spring ioc-container


    【解决方案1】:

    util namespace 有一些非常有用的东西。

    你可以使用&lt;util:property-path id="name" path="testBean.age"/&gt;

    【讨论】:

      【解决方案2】:

      在 Spring 3 中你也可以使用Spring Expression language:

      <bean name="beanB" class="...">
           <property name="propertyB"><value>#{ beanA.propertyA }</value></property>
      </bean>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多