简体   繁体   English

如何从其他bean属性值设置bean属性值

[英]How to set bean property value from other bean property value

For example we have bean beanA with string property propertyA : 例如,我们有带字符串属性propertyA的 bean beanA

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

And second bean beanB has also string property propertyB, and I want initialize this property with same value as beanA.propertyA, I thought I need to do something like this: 第二个bean beanB也具有字符串属性propertyB,并且我想使用与beanA.propertyA相同的值来初始化此属性,我认为我需要执行以下操作:

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

But this is not works, expression beanA.propertyA threated as string value. 但这是行不通的,表达式beanA.propertyA威胁为字符串值。

Probably this is go against IoC theory and not supported by Spring. 可能这与IoC理论背道而驰,Spring不支持。

Thanks. 谢谢。

The util namespace has some pretty useful things. util名称空间具有一些非常有用的功能。

You can use <util:property-path id="name" path="testBean.age"/> 您可以使用<util:property-path id="name" path="testBean.age"/>

In Spring 3 you can also use Spring Expression language : 在Spring 3中,您还可以使用Spring Expression语言

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM