Nimrod Dayan
Nimrod Dayan

Reputation: 3100

Is it possible to use Spring EL when referencing a bean in constructor-arg?

<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>

I get the following error:

Cannot resolve reference to bean '#{(config.isFooEnabled()) ? foo : bar}'

Upvotes: 0

Views: 478

Answers (1)

Tomas Narros
Tomas Narros

Reputation: 13468

The ref attribute value must be a string, not a reference itself, so, I would try returning foo or bar as a string, as in:

<constructor-arg ref="#{(config.isFooEnabled()) ? 'foo' : 'bar'}"/>

The spring beans engine will get the string returned by the expression, and resolve the reference.

Upvotes: 2

Related Questions