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
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.
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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With