Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load system properties file in Spring?

I have a properties file which I would like loaded in to System Properties so that I can access it via System.getProperty("myProp"). Currently, I'm trying to use the Spring <context:propert-placeholder/> like so:

<context:property-placeholder location="/WEB-INF/properties/webServerProperties.properties" />

However, when I try to access my properties via System.getProperty("myProp") I'm getting null. My properties file looks like this:

myProp=hello world

How could I achieve this? I'm pretty sure I could set a runtime argument, however I'd like to avoid this.

Thanks!

like image 303
Polaris878 Avatar asked Nov 07 '10 06:11

Polaris878


1 Answers

In Spring 3 you can load system properties this way:

  <bean id="systemPropertiesLoader"
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" value="#{@systemProperties}" />
    <property name="targetMethod" value="putAll" />
    <property name="arguments">
        <util:properties location="file:///${user.home}/mySystemEnv.properties" />
    </property>
</bean>
like image 55
Florin Avatar answered Oct 13 '22 17:10

Florin