1

I would like to conditionally/dynamically display the <value>classpath:/resources/three.sql</value> value node in my spring xml configuration based on a system property, i.e. ${env}. Something like if ${env} is dev then include it else exclude it or if thats not easy then just conditionally pass in the all the value nodes from a system property. Is this possible?

<bean id="myBean"
          class="com.testMyBean">
        <property name="scripts">
            <list>
                <value>classpath:/resources/one.sql</value>
                <value>classpath:/resources/two.sql</value>
                <value>classpath:/resources/three.sql</value>
            </list>
        </property>
</bean>
c12
  • 9,557
  • 48
  • 157
  • 253

2 Answers2

3

You can use Spring profiles to conditionally include the beans.

Add a beans wrapper element with a profile specified:

<beans profile="dev">
    <bean id="myBean" class="com.testMyBean">
        <property name="scripts">
            <list>
                <value>classpath:/resources/one.sql</value>
                <value>classpath:/resources/two.sql</value>
                <value>classpath:/resources/three.sql</value>
            </list>
        </property>
    </bean>
</beans>

Then set this system property (VM argument) in your runtime configuration:

-Dspring.profiles.active="dev"

To specify the default profile for the application you can set a context parameter in your web.xml file:

<context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>openshift</param-value>
</context-param>
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • is there a way to accept a profile as a default and thusly not supply the -Dspring.profiles.active in that env and spring picks up that profile? – c12 Jan 20 '14 at 13:04
  • 1
    @c12 See the update. This is simply a `context-param` in the `web.xml`. I use this when deploying to the open shift cloud. – Kevin Bowersox Jan 20 '14 at 13:08
  • I have that code on GitHub for review: https://github.com/kmb385/toThought/blob/github_master/src/main/webapp/WEB-INF/spring/spring-data-context.xml – Kevin Bowersox Jan 20 '14 at 13:14
  • my aim is to only include a particular script in the users local environment and not on any test or production application servers. I would prefer to somehow base the loading of the script from a .properties file since we already have to do that for other things. Is it possible to based the profile on a .properties file value? – c12 Jan 20 '14 at 13:32
  • @c12 I do not believe this is possible, see: http://stackoverflow.com/questions/18614849/how-to-set-the-profile-using-application-properties-in-spring – Kevin Bowersox Jan 20 '14 at 13:39
1

Best way do it use profiles, as said above.

You can use multiple xml-files, where file name is parametrized ${env} variable.

root xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    <import resource="classpath:etc/abc-${env}.xml" />
   ...
<beans

abc-three.xml:(if ${env} == three)

<bean id="myBean" class="com.testMyBean">
    <property name="scripts">
        <list>
            <value>classpath:/resources/one.sql</value>
            <value>classpath:/resources/two.sql</value>
            <value>classpath:/resources/three.sql</value>
        </list>
    </property>
</bean>

abc-two.xml:(if ${env} == two)

<bean id="myBean" class="com.testMyBean">
    <property name="scripts">
        <list>
            <value>classpath:/resources/one.sql</value>
            <value>classpath:/resources/two.sql</value>
        </list>
    </property>
</bean>
Sergey Morozov
  • 4,528
  • 3
  • 25
  • 39