[ACCEPTED]-How do I read JVM arguments in the Spring applicationContext.xml-jvm-arguments

Accepted answer
Score: 27

You can use Spring EL expressions, then 9 it is #{systemProperties.test} for -Dtest="hallo welt"

In your case it should be:

<bean id="myBean" class="com.foo.bar.myClass">
  <property name="environment">
    <value>#{systemProperties.environment}</value>
  </property>
</bean>

The # instead of $ is no mistake!

$ would 8 refer to place holders, while # refers to 7 beans, and systemProperties is a bean.


May it is only a spelling 6 error, but may it is the cause for your 5 problem: In the example for your command 4 line statement you name the variable env

(-Denv=development, for 3 example...

But in the spring configuration 2 you name it environment. But both must be equals of 1 course!

Score: 10

If you register a PropertyPlaceholderConfigurer 4 it will use system properties as a fallback.

For 3 example, add

<context:property-placeholder/>

to your configuration. Then 2 you can use ${environment} in either your XML configuration 1 or in @Value annotations.

Score: 4

You can load a property file based on system 4 property env like this:

   <bean id="applicationProperties"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="ignoreResourceNotFound" value="false" />
      <property name="ignoreUnresolvablePlaceholders" value="true" />
      <property name="searchSystemEnvironment" value="false" />
      <property name="locations">
         <list>
            <value>classpath:myapp-${env:prod}.properties</value>
         </list>
      </property>
   </bean>

If env is not set default 3 it to production otherwise development and 2 testing teams can have their flavor of app 1 by setting -Denv=development or -Denv=testing accordingly.

Score: 1

Use #{systemProperties['env']}. Basically pass the propertyName used 2 in the Java command line as -DpropertyName=value. In this case 1 it was -Denv=development so used env.

Score: 0

Interestingly, Spring has evolved to handled 10 this need more gracefully with PropertySources: http://spring.io/blog/2011/02/15/spring-3-1-m1-unified-property-management/

With 9 a few configurations and perhaps a custom 8 ApplicationInitializer if you are working 7 on a Web app, you can have the property 6 placeholder handle System, Environment, and 5 custom properties. Spring provides PropertySourcesPlaceholderConfigurer 4 which is used when you have in your Spring 3 config. That one will look for properties 2 in your properties files, then System, and 1 then finally Environment.

Score: 0

Spring 3.0.7

<context:property-placeholder location="classpath:${env:config-prd.properties}" />

And at runtime set: -Denv=config-dev.properties

If 1 not set "env" will use default "config-prd.properties".

More Related questions