Tomer Kfir Tomer Kfir - 1 year ago 148
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/"),

Answer Source

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/")})

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download