Reputation: 86905
I'd like to configure Spring
to override some properties during production, but only(!) if the production.properties
file is found and only for the properties it defines. But these contraints should only apply to this production file. All other property files should be required.
But I cannot import two different property resources within a spring configuration. What would I have to change?
@Configuration
@PropertySource({"classpath:default.properties"})
@PropertySource({"file:production.properties"}, ignoreResourceNotFound = true) //Error: Duplicate annotation
Upvotes: 2
Views: 1497
Reputation: 64079
You can do the following in your configuration file:
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
final PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new Resource[ ] {
new ClassPathResource( "default.properties" ),
new FileSystemResource("production.properties")
};
pspc.setLocations( resources );
pspc.setIgnoreResourceNotFound(true);
pspc.setIgnoreUnresolvablePlaceholders(false);
return pspc;
}
Upvotes: 0
Reputation: 136112
Try PropertySources
annotation, it's a container annotation that aggregates several PropertySource
annotations.
Upvotes: 2