Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use property-placeholder for file on filesystem

Tags:

java

spring

We used to have a way to load properties from a file on the classpath:

<context:property-placeholder location="classpath:myConfigFile.properties" />

and it worked great. But now we want to load properties from a specific file on the system that is NOT in the classpath. We wanted to be able to dynamically load the file, so we are using a Java environment variable to populate it. I'll give a simple example below:

In Java:

  System.setProperty("my.prop.file", "/path/to/myConfigFile.properties");

In Spring XML:

<context:property-placeholder location="${my.prop.file}" />

I've also tried it this way, thanks to an idea from Luciano:

<context:property-placeholder properties-ref="prop" />

<util:properties id="prop" location="reso"/>

<bean id="reso" class="org.springframework.core.io.FileSystemResource">
    <constructor-arg index="0" value="${my.prop.file}" />
</bean>

Everything I've tried has failed. No matter what I set my.prop.file to. Greatest hits include:

<context:property-placeholder location="/path/to/myConfigFile.properties" />
(ClassNotFoundException: .path.to.myConfigFile.properties)

<context:property-placeholder location="file:/path/to/myConfigFile.properties" />
(ClassNotFoundException: file:.path.to.myConfigFile.properties)

<context:property-placeholder location="file:///path/to/myConfigFile.properties" />
(ClassNotFoundException: file:...path.to.myConfigFile.properties)

How do you use property-placeholders with a location that is on the file system and NOT on the classpath? We are using Spring 3.0.5.

It turns out there was a problem with the script running the Java program that loads the spring file. Thank you for helping. I am going to request that this question be deleted, as the original code works after all. Thank you for your help.

like image 470
black panda Avatar asked May 10 '12 19:05

black panda


2 Answers

This did work for me:

<context:property-placeholder location="file:/path/to/myConfigFile.properties" />

But this (interestingly) did not:

<context:property-placeholder location="#{ systemProperties['foo'] }" />

I got

java.io.FileNotFoundException: Could not open ServletContext resource [/#{ systemProperties['foo'] }]

You're not going to be able to use a property placeholder ${..} with the definition of a PropertyPlaceholderConfigurer. It's the chicken before the egg.

You can always subclass PropertyPlaceholderConfigurer and have it do whatever you want (for example, call setLocations() in a @PostConstruct method. Then, instead of using <context:property-placeholder>, use:

<bean class="com.foo.MyPropertyPlaceholderConfigurer"/>
like image 152
sourcedelica Avatar answered Nov 16 '22 17:11

sourcedelica


What about this way?

<context:property-placeholder properties-ref="prop" />

<util:properties id="prop" location="reso"/>

<bean id="reso" class="org.springframework.core.io.FileSystemResource">
    <constructor-arg index="0" value="/yourpathandfile" />
</bean>
like image 31
Luciano Avatar answered Nov 16 '22 16:11

Luciano