membersound
membersound

Reputation: 86637

How to import optional properties file for production in Spring config?

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: 1487

Answers (2)

geoand
geoand

Reputation: 63991

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

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

Try PropertySources annotation, it's a container annotation that aggregates several PropertySource annotations.

Upvotes: 2

Related Questions