Ruslan Akhundov
Ruslan Akhundov

Reputation: 2216

Loading additional spring profiles from java config

Is there any possibility to load aditional spring profiles from java config?

I know that I can use -Dspring.profile.active argument and also add profiles to spring.profiles.include in application.properties.

What I need is to be able to activate profiles from java config. I've created PropertyPlaceholderConfigurer, where I'm adding some custom property files, which also contains property spring.profiles.include, all properties are load and it works ok, but spring doesn't activate any profiles which are inclded using this property.

@Bean
public static PropertyPlaceholderConfigurer ppc() throws IOException {
    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    ppc.setLocations(new ClassPathResource("properties/" + property + ".properties"));
    ppc.setIgnoreUnresolvablePlaceholders(true);
    return ppc;
}

Upvotes: 2

Views: 860

Answers (1)

Iulian Rosca
Iulian Rosca

Reputation: 1015

The active spring profiles are defined in properties via the following configuration: spring.profiles.active:.

You should list in all the files that you import the profiles that they activate via the above configuration key.

EDIT

First, as per the official documentation the configuration spring.profiles.include is more suitable for unconditionally adding active profiles.

Second, I can assume that PropertyPlaceholderConfigurer is not suitable for what you want to achieve. The official documentation lists the ways you can Externalize Configuration. You can try to use @PropertySource:

@PropertySources({
        @PropertySource(value = "classpath:application.properties"),
        @PropertySource(value = "classpath:other.properties", ignoreResourceNotFound = true)
})
public class Application {
       ...
    }
}

Additionally, you can try to list the other properties files in property spring.config.location inside application.properties as described here.

Upvotes: 1

Related Questions