How do I read JVM arguments in the Spring applicationContext.xml
Asked Answered
T

6

21

I have a JSF web application with Spring and I am trying to figure out a way to reference the JVM arguments from the applicationContext.xml. I am starting the JVM with an environment argument (-Denv=development, for example). I have found and tried a few different approaches including:

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

But, when the setter method is invoked in MyClass, the string "${environment}" is passed, instead of "development". I have a work around in place to use System.getProperty(), but it would be nicer, and cleaner, to be able to set these values via Spring. Is there any way to do this?

Edit: What I should have mentioned before is that I am loading properties from my database using a JDBC connection. This seems to add complexity, because when I add a property placeholder to my configuration, the properties loaded from the database are overridden by the property placeholder. I'm not sure if it's order-dependent or something. It's like I can do one or the other, but not both.

Edit: I'm currently loading the properties using the following configuration:

<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc.mydb.myschema"/> 
</bean>

<bean id="props" class="com.foo.bar.JdbcPropertiesFactoryBean">
    <property name="jdbcTemplate">
        <bean class="org.springframework.jdbc.core.JdbcTemplate">
            <constructor-arg ref="myDataSource" />
        </bean>
    </property>
</bean>

<context:property-placeholder properties-ref="props" />
Tale answered 8/4, 2011 at 16:43 Comment(0)
M
27

You can use Spring EL expressions, then 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 refer to place holders, while # refers to beans, and systemProperties is a bean.


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

(-Denv=development, for example...

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

Murrelet answered 8/4, 2011 at 16:58 Comment(2)
I get the same result using this approach as I do using the approach in my example. The string, "#{systemProperties.environment}" is what gets passed to the setter method.Tale
@jinxed: I think I have found the last problem, there was a spelling mistake. - see my extended answer.Murrelet
J
10

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

For example, add

<context:property-placeholder/>

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

Jaquith answered 8/4, 2011 at 17:22 Comment(13)
When I add this to the configuration, the properties I load from the database are overridden by this property placeholder. Edited original description.Tale
How are the database properties loaded? Are they part of the PropertyPlaceholderConfigurer configuration?Jaquith
They are loaded via JDBC using the configuration shown in the question (above)...Tale
Sorry, I don't see where how they are loaded?Jaquith
Ok, when you say the properties are overridden by the property placeholder, do you mean the ${environment} property placeholder specifically? Or all properties? How are you referencing the properties that you load from the database?Jaquith
When I add the property placeholder either through <context:property-placeholder/> or through <bean class="org.springframework.web.context.support.PropertyPlaceholderConfigurer"> the properties I loaded from the database seem to be wiped out. Those properties (DB) are referenced in the Spring config by using EL (value="${REF_SERVICE.WS_URL}") for property values defined for other beans in the config.Tale
I would recommend wiring "props" into another bean just to verify that com.foo.bar.JdbcPropertiesFactoryBean is actually loading the properties correctly.Jaquith
I wired props into another bean and verified that all properties are being loaded correctly. That part is working as designed.Tale
So none of the placeholders (eg. ${xxxx}) are working? What are they resolving to?Jaquith
The placeholders for properties loaded from the db are working properly. The placeholders for JVM args are not working, they do not get resolved. Instead, they are passed as a string to the bean's accessor. For example, if I have value="${environment}" in the config, the string "${environment}" gets passed to the bean instead of "development".Tale
What happens if you don't pass -Denvironment=development to the JVM and instead you put that value in the DB?Jaquith
Interesting. The next step I would recommend is playing with different values of system-properties-mode, eg, <context:property-placeholder system-properties-mode="OVERRIDE"/>. Otherwise it sounds like a bug in Spring. Stepping through the Spring code with a debugger would provide the definitive answer.Jaquith
This should be accepted as proper answer - it allows you to define xxx property in you config file (xxx=val1) and adds the possibility to override this value via command line -Dxxx=val2. Your code then contains only one - ${xxx} - reference. To make this work you have to define 2 <context:property-placeholder ..> with appropriate order attribute.Zimmermann
N
4

You can load a property file based on system 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 it to production otherwise development and testing teams can have their flavor of app by setting -Denv=development or -Denv=testing accordingly.

Necrophobia answered 11/4, 2011 at 21:45 Comment(3)
Read the thread again. The properties are being loaded from the DB. The question is, how do I use the JVM arguments elsewhere in the config?Tale
This is what I see in your question: I am starting the JVM with an environment argument (-Denv=development, for example) I also noted in your edited question you mentioned that you are reading some properties from DB as well. Is there any restriction that properties can be read from ONLY 1 source?Necrophobia
All properties are loaded from the database. I understand the concept of using PropertyPlaceholderConfigurer to pull in JVM arguments, but when I use the PropertyPlaceholderConfigurer, the DB properties that had been successfully loaded, are now missing.Tale
S
1

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

Shelly answered 24/11, 2015 at 4:51 Comment(0)
M
0

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

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

Mintamintage answered 3/5, 2015 at 21:23 Comment(0)
P
0

Spring 3.0.7

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

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

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

Philippic answered 16/9, 2015 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.