Over a million developers have joined DZone.
Platinum Partner

Use Spring to create System properties

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

That’s an old trick, but it helped me today, and I’m pretty sure I’ll use it again.

You can setup some System properties with Spring.
Mix that with Maven resources filtering and you can execute your application with some specific System properties according to your profile.

In your applicationContext.xml, add:

 <bean>
    <property name="targetClass" value="java.lang.System"/>
    <property name="targetMethod" value="setProperty"/>
    <property name="arguments">
      <list>
        <value>logsDir</value>
        <value>${logs.dir}</value>
      </list>
    </property>
  </bean>

Here ${logs.dir} is replaced before the execution of the application. During the build phase, maven will look at the properties you’ll have defined in your profile, and use them to filter the files you will have set (in this example, we set maven to filter all files in src/main/resources, where applicationContext.xml is located.

For instance, we can define a property: logs.dir=/mydevmachine/logs for a dev profile, and logs.dir=/myprodmachine/logs for a prod profile.

Then in your application, doing a System.getProperty(“logsDir”) will give you the correct value according to your maven profile.

No need to add System properties to MAVEN_OPTS anymore!

From http://jsoftbiz.wordpress.com/2011/12/08/use-spring-to-create-system-properties/

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}