Conditional statement inside Spring config

后端 未结 4 2015
说谎
说谎 2020-12-06 00:33

How to have conditional statement within a spring configuration file

I have String bean (b) whose value depends on the value of a property (a). a is set dynamically

相关标签:
4条回答
  • 2020-12-06 00:35

    As Ryan said SpEL can help. You should be able to do something like this in Spring xml:

    <bean id="flag" class="java.lang.Boolean">
        <constructor-arg value="#{ systemProperties['system.propery.flag'] ?: false }" />
    </bean>
    
    <bean id="bean" class="com.my.MyBean">
        <property name="property" value="#{ flag ? 'yes' : 'no' }"/>
    </bean>
    
    0 讨论(0)
  • 2020-12-06 00:40

    Try this...It works.. Given Roll,Location,name is in property file and I am reading it above this line.

    <bean id="Student" class="beans.Student"> <property name="name" value="#{ ${Roll}== 1 ? '${Location}' : '${name}' }"/> </bean>

    0 讨论(0)
  • 2020-12-06 00:44

    See Spring Expression Language for Spring 3+. Otherwise, you're probably stuck with writing a FactoryBean or something similar.

    0 讨论(0)
  • 2020-12-06 00:58

    below is working for me. system property passed as java -Dflag=true -jar project.jar

     <bean id="flag" class="java.lang.Boolean">
        <constructor-arg value="#{ systemProperties['flag'] ?: false }" />
      </bean>
    
      <bean id="bean" class="com.my.MyBean">
        <property name="property" value="#{ flag ? 'yes' : 'no' }"/>
     </bean>
    
    0 讨论(0)
提交回复
热议问题