views:

119

answers:

1

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

+1  A: 

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.

skaffman
Nice solution - thanks. The only problem (and I admit this isn't a serious problem at all) is that sometimes it's useful to be able to view/edit the properties file directly - eg in an emergency situation, or to do some quick testing. Having it inside the jar doesn't prevent this, but makes it a little cumbersome.
stevec
@stevec: It doesn't have to be inside a JAR, file, it can just as easily be inside a filesystem directory that's on the classpath.
skaffman