Tomer Kfir Tomer Kfir - 5 months ago 66
Java Question

Spring - how to override internal config file with external file

In my spring project I'm having two different conf files which have the same properties.

One of them is in an external directory while the other one is inside the resource of my project.

I want that the external file's properties to override the internal values (if they exist in the external file).

In my configuration class I set the files with the PropertySource annotation:

{ @PropertySource("file:${HOME}/conf/"),


The order of the declared files is important. If the same key is defined in two or more files, the value associated with the key in the last declared file will override any previous value(s).

From spring docs:

In cases where a given property key exists in more than one .properties file, the last @PropertySource annotation processed will 'win' and override.

So just swap @PropertySource("file:${HOME}/conf/") with @PropertySource("classpath:/")

Example: @PropertySources({@PropertySource("classpath:/"),@PropertySource("file:${HOME}/conf/")})