<bean id="foo" class="com.bla.Foo" />
<bean id="bar" class="com.bla.Bar" />
<bean id="channel" class="com.bla.Channel">
<constructor-arg ref="#{(config.isFooEnabled()) ? foo : bar}"/>
</bean>
我收到以下错误:
Cannot resolve reference to bean '#{(config.isFooEnabled()) ? foo : bar}'
最佳答案
ref
属性值必须是字符串,而不是引用本身,因此,我会尝试将 foo
或 bar
作为字符串返回,如:
<constructor-arg ref="#{(config.isFooEnabled()) ? 'foo' : 'bar'}"/>
Spring beans引擎将获取表达式返回的字符串,并解析引用。
关于java - 在构造函数参数中引用 bean 时是否可以使用 Spring EL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13722838/