java - Spring IoC: Conditional Injection in run time -


how can conditionally inject bean externally using framework ( not creating factory class)?

in below scenario both childbeans instantiated while injected parent bean in run time based on condition.

<bean id=childbean1> <bean id=childbean2> <parentbean name='parentbean' lazy-init="true">    <property name='flag'>     <somecondition flag=1/>      <property name='child' ref ='childbean1'/>    <somecondition flag=2/>     <property name='child' ref ='childbean2'/> </parentbean> 

you can via spring expression language(spel):

<bean class="com.example.spring.testbean">     <property name="dependency" value="#{systemproperties['profile'] == 'test' ? dependencya : dependencyb}" /> </bean> 

also possible using java config below:

@bean public hellobean hellobean() {     hellobean hellobean = new hellobean ();     if (condition) {         hellobean.setdependency(dependencya());     } else {         hellobean.setdependency(dependencyb());     }     return hellobean; } 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -