spring:通过读取另一个bean的属性来设置一个bean的属性? [英] spring: set property of one bean by reading the property of another bean?
问题描述
可以通过读取另一个bean的属性来设置一个bean的属性吗?例如,假设我有:
Is it possible to set the property of one bean by reading the property of another bean? For instance, suppose I had:
class A {
void setList(List list);
}
class B {
List getList();
}
我希望Spring实例化两个类,并调用A的setList方法,传递在调用B的getList方法的结果中。 Spring配置可能如下所示:
I would like Spring to instantiate both classes, and call A's setList method, passing in the result of calling B's getList method. The Spring configuration might look something like:
<bean id="b" class="B"/>
<bean id"a" class="A">
<property name="list" ref="b" ref-property="list"/>
</bean>
唉,这个组成的XML不起作用。
Alas, this made-up XML does not work.
为什么不把B注入A?因为我不想引入额外的依赖。 A只是依赖列表,而不是B。
Why not just inject B into A? Because I do not want to introduce the extra dependency. A is only dependent List, not on B.
推荐答案
除了凯文的回答,如果你使用的是弹簧3.0,使用新的弹簧表达式语言来执行此操作
in addition to @Kevin's answer if you are using spring 3.0 it is possible to do this with the new spring expression language
<bean id="a" class="A">
<property name="list"
value="#{b.list}"/>
</bean>
这篇关于spring:通过读取另一个bean的属性来设置一个bean的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!