开发者

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 开发者_如何学Cprocess 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


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.


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


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>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜