Over a million developers have joined DZone.

Use Spring to create System properties

· Java Zone

Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience, brought to you in partnership with JetBrains

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/

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}