Tomer Kfir Tomer Kfir - 10 days ago 5
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:

@PropertySources(
{ @PropertySource("file:${HOME}/conf/application.properties"),
@PropertySource("classpath:/data.properties")
})

Answer

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/application.properties") with @PropertySource("classpath:/data.properties")

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

Comments