1

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.

3 Answers3

1

This will work

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

Combined with

-Ddomain=dev

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

Essex Boy
  • 7,565
  • 2
  • 21
  • 24
1

@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>
kalsowerus
  • 998
  • 8
  • 23
  • ${domain} will give the value of the domain bean. But what I want is , get the value of the bean whose name is the value stored in domain bean. Domain = prod /test So based on domain value, I would want to refer to prod bean or test bean – Enigma girl Jul 04 '17 at 09:30
  • @Enigmagirl Your prod/test beans are both Strings, why won't one String with either value work? – kalsowerus Jul 04 '17 at 09:31
  • Either of them will work. When I get the value of domain as Prod, I want Base.Prod to be assigned for Bean X, when domain value is test then I want Base.Beta to be assigned for Bean X. It is conditional and I do not know how conditional is handled when using xmls – Enigma girl Jul 04 '17 at 09:36
  • @Enigmagirl including different XMLs for each environment might be the only option if you can't use the value of `${domain}` directly... – kalsowerus Jul 04 '17 at 09:39
  • Alas! I wonder how conditional beans were handled before. Like if something is null, then initialize a bean with abc value, else initialize xyz value. – Enigma girl Jul 04 '17 at 09:45
0

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>