Enigma girl
Enigma girl

Reputation: 45

How to create bean by double reference?

I do not know how to articulate/search the issue in the first place.

So the bean initialized are :

<bean id='domain' factory-bean='appConfig' factory-method='getDomain'/
<bean id='prod' class='java.lang.String'> <constructor-arg value='Base.Prod'/> </bean>
<bean id='test' class='java.lang.String'> <constructor-arg value='Base.Beta' /> </bean>

Now I need to create a bean "X" using the value of Prod or Test depending on the value of domain. If domain is Prod,use the bean of Prod to initialize bean X else use Test.

${${domain}} does not work. I tried searching but was not able to find a question that meant this.

Upvotes: 1

Views: 139

Answers (3)

Enigma girl
Enigma girl

Reputation: 45

Finally found the answer. There is a way to refer the test/prod bean through domain. You just got to use #{domain}. More information here: spEL

Use it as :

<bean id="ThatBean" class="whicheverClass">   
 <constructor-arg ref="#{domain}" /> 
</bean>

Upvotes: 0

kalsowerus
kalsowerus

Reputation: 1008

@EssexBoy's answer in XML would look something like this:

<bean id="domain" class="java-lang.String">
    <constructor-arg>
        <value>${domain}</value>
    </construcotr-arg>
</bean>

Upvotes: 1

Essex Boy
Essex Boy

Reputation: 7950

This will work

@Value("${domain}")
private String domain;

Combined with

-Ddomain=dev

Spring adds any system properties to properties read from properties file.

Upvotes: 1

Related Questions