stevec
stevec

Reputation: 556

Reference Spring properties file using path relative to config file

I am moving properties from inside my Spring config file to a separate properties file. This is included in the config file with

<bean class="org.springframework.beans.factory.config.PropertyPlaceHolderConfigurer">
  <property name="location" value="file:properties/${CONFIG_MODE}/service.properties" />
</bean>

As it stands, the location of the properties file is relative to the current working directory of the server process.

This creates the requirement that the process must be started from a specific working directory, and even worse allows for the (admittedly remote) possibility that it could pick up an entirely different properties file - for example if it was started with the working directory set to an older version of the service.

I'd like to reference the properties file using a path that is relative to the directory containing the config file.

Looking at FileSystemResource, it seems createRelative might be what I need, but I can't figure out how to use it in the config file.

Thanks,

Steve

Upvotes: 13

Views: 36669

Answers (3)

A-Coder
A-Coder

Reputation: 1

Supposing you have placed the config.properties file inside WEB-INF Then:

<bean id="propertyConfigurerInternal"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
    <value>classpath:../config.properties</value>
</property>

Upvotes: 0

Craig Swing
Craig Swing

Reputation: 8162

Using 3.1, you can keep the files off of the classpath if you want.

With the following bean definition,

<bean class=
    "org.springframework.beans.factory.config.PropertyPlaceHolderConfigurer">
  <property name="location" 
    value="file:${props.path}/service.properties" />
</bean>

you can set a property using the java command line

java ... -Dprops.path=path/to/where/it/is

Upvotes: 10

skaffman
skaffman

Reputation: 403441

I don't know of a way to do that.

What you can do, however, is load the properties file from the classpath:

<bean class="org.springframework.beans.factory.config.PropertyPlaceHolderConfigurer">
  <property name="location" value="classpath:path/to/service.properties" />
</bean>

The classpath location of your properties file is a far more predictable situation, and it'll work as long as your classpath is set up properly.

Upvotes: 11

Related Questions