Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 917
stevec Avatar asked Aug 31 '10 16:08

stevec


People also ask

How do you externalize a constants from a Spring configuration file into a properties file?

You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring's Environment abstraction or bound to structured objects.

How do I add a local path to a property file?

when loading the properties from the file with the method Properties. load(), I need to use the escape character '\' in the path. So my path looks like: C:\\Users\\Harald\\Folder1\\Version1\\Folder2 . And it works this way, no exception is thrown.

How do you call external properties from spring boot?

properties as the configuration file name you can switch to another by specifying a spring.config.name environment property. You can also refer to an explicit location using the spring. config. location environment property (comma-separated list of directory locations, or file paths).


2 Answers

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.

like image 162
skaffman Avatar answered Nov 15 '22 16:11

skaffman


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
like image 28
Craig Swing Avatar answered Nov 15 '22 16:11

Craig Swing