How to load system properties file in Spring?
Asked Answered
B

4

9

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!

Bray answered 7/11, 2010 at 6:4 Comment(1)
Perhaps this related question gives some direction?Grodno
D
10

While I subscribe to the Spirit of Bozho's answer, I recently also had a situation where I needed to set System Properties from Spring. Here's the class I came up with:

Java Code:

public class SystemPropertiesReader{

    private Collection<Resource> resources;

    public void setResources(final Collection<Resource> resources){
        this.resources = resources;
    }

    public void setResource(final Resource resource){
        resources = Collections.singleton(resource);
    }

    @PostConstruct
    public void applyProperties() throws Exception{
        final Properties systemProperties = System.getProperties();
        for(final Resource resource : resources){
            final InputStream inputStream = resource.getInputStream();
            try{
                systemProperties.load(inputStream);
            } finally{
                // Guava
                Closeables.closeQuietly(inputStream);
            }
        }
    }

}

Spring Config:

<bean class="x.y.SystemPropertiesReader">

    <!-- either a single .properties file -->
    <property name="resource" value="classpath:dummy.properties" />

    <!-- or a collection of .properties file -->
    <property name="resources" value="classpath*:many.properties" />

    <!-- but not both -->

</bean>
Darees answered 7/11, 2010 at 10:49 Comment(0)
V
18

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>
Vanessa answered 26/10, 2011 at 1:24 Comment(0)
P
10

The point is to do this the other way around - i.e. use system properties in spring, rather than spring properties in the system.

With PropertyPlaceholderConfigurer you get your properties + the system properties accessible via the ${property.key} syntax. In spring 3.0 you can inject these using the @Value annotation.

The idea is not to rely on calls to System.getProperty(..), but to instead inject your property values. So:

@Value("${foo.property}")
private String foo;

public void someMethod {
    String path = getPath(foo);
    //.. etc
}

rather than

public void someMethod {
    String path = getPath(System.getProperty("your.property"));
    //.. etc
}

Imagine you want to unit test your class - you'd have to prepopulate the System object with properties. With the spring-way you'd just have to set some fields of the object.

Placet answered 7/11, 2010 at 9:0 Comment(2)
Is there also a way to grab the property programatically instead of using the annotation with Spring Expression syntax? for example: someSpringApi.getProperty("${foo.property}")Bray
Yes - static.springsource.org/spring/docs/3.0.0.M3/…Placet
D
10

While I subscribe to the Spirit of Bozho's answer, I recently also had a situation where I needed to set System Properties from Spring. Here's the class I came up with:

Java Code:

public class SystemPropertiesReader{

    private Collection<Resource> resources;

    public void setResources(final Collection<Resource> resources){
        this.resources = resources;
    }

    public void setResource(final Resource resource){
        resources = Collections.singleton(resource);
    }

    @PostConstruct
    public void applyProperties() throws Exception{
        final Properties systemProperties = System.getProperties();
        for(final Resource resource : resources){
            final InputStream inputStream = resource.getInputStream();
            try{
                systemProperties.load(inputStream);
            } finally{
                // Guava
                Closeables.closeQuietly(inputStream);
            }
        }
    }

}

Spring Config:

<bean class="x.y.SystemPropertiesReader">

    <!-- either a single .properties file -->
    <property name="resource" value="classpath:dummy.properties" />

    <!-- or a collection of .properties file -->
    <property name="resources" value="classpath*:many.properties" />

    <!-- but not both -->

</bean>
Darees answered 7/11, 2010 at 10:49 Comment(0)
B
0
@Configuration
Public class YourAppConfig {

    @Value("${your.system.property}") String prop
    @Autowired
    Public void LoadProperties() {
        System.setProperty(PROPERTY_NAME, prop) ;
    }

    @Bean
    ...
Buckeye answered 18/3, 2022 at 2:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.