Home Ask Login Register

Developers Planet

Your answer is one click away!

Dmitrii Adonin February 2016

How to refer to separate @PropertySource in @PropertySources to get value

I have a couple property files, described in configuration

@Configuration    
@PropertySources({
        @PropertySource(name="p1", value = "classpath:p1.properties"),
        @PropertySource(name="p2", value = "classpath:p2.properties")})

Both files have properties with the same keys and different values, for example:

prop1=11
prop2=12

and

prop1=21
prop2=22

How to refer to the right property source to use value? I mean smth like

@Value("${p1.prop1}")
private int prop11;

@Bean
public SomeBean someBean() {
    return new SomeBean(prop11);
}

but @Value("${p1.prop1}") is my wrong attempt.

Answers


matsev February 2016

You can't. The two property sources will be merged into the same Spring Environment. The last declared value for the same key in the your .properties files will override any previous value for the same key. If your read the JavaDoc of @PropertySource you will find the following statement:

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

Post Status

Asked in February 2016
Viewed 2,525 times
Voted 5
Answered 1 times

Search




Leave an answer


Quote of the day: live life