3

I am using spring xml configuration and want to load either bean if system environment property is set


For example ::

if(system.property is set as "A")
    <bean id="A" class="mypackage.A">
    </bean>
else
    <bean id="B" class="mypackage.B">
    </bean>

Is it possible with SpEl , similar post i found is Condition Bean loading but not of if else condition as in this post we are using lazy initialization of loading bean based on if variable is present in system environment but no if,else condition is specified for bean loading.

Please share if anybody has any idea how to achieve this.

Anand Kadhi
  • 1,790
  • 4
  • 27
  • 40

1 Answers1

0

You can use spring bean profiles

     <beans profile="A">
       <bean id="A" class="mypackage.A"></bean>
     </beans>

     <beans profile="B">
       <bean id="A" class="mypackage.B"></bean>
     </beans>

To activate one of these profiles you can set value of system property spring.profiles.active to A or B

You can also use conditional bean filtering support provided in spring 4 http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-java-conditional

  • Actually i forgot to mention that i wanted to avoid profiling for loading only one conditional bean, anyways thanks for your input. – Anand Kadhi Oct 23 '15 at 04:56