Issue
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!
Solution
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>
Answered By - Sean Patrick Floyd
Answer Checked By - Katrina (JavaFixing Volunteer)