How to set active spring 3.1 environment profile via a properites file and not via an env variable or system property How to set active spring 3.1 environment profile via a properites file and not via an env variable or system property spring spring

How to set active spring 3.1 environment profile via a properites file and not via an env variable or system property


In web.xml

<context-param>    <param-name>spring.profiles.active</param-name>    <param-value>profileName</param-value></context-param>

Using WebApplicationInitializer

This approach is used when you don't have a web.xml file in Servlet 3.0 environment and are bootstrapping the Spring completely from Java:

class SpringInitializer extends WebApplicationInitializer {    void onStartup(ServletContext container) {        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();        rootContext.getEnvironment().setActiveProfiles("profileName");        rootContext.register(SpringConfiguration.class);        container.addListener(new ContextLoaderListener(rootContext));    }}

Where SpringConfiguration class is annotated with @Configuration.


The answer from Thomasz is valid as long as the profile name can be provided statically in the web.xml or one uses the new XML-less configuration type where one could programmatically load the profile to set from a properties file.

As we still use the XML version I investigated further and found the following nice solution where you implement your own ApplicationContextInitializer where you just add a new PropertySource with a properties file to the list of sources to search for environment specific configuration settings. in the example below one could set the spring.profiles.active property in the env.properties file.

public class P13nApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {    private static Logger LOG = LoggerFactory.getLogger(P13nApplicationContextInitializer.class);    @Override    public void initialize(ConfigurableApplicationContext applicationContext) {        ConfigurableEnvironment environment = applicationContext.getEnvironment();        try {            environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:env.properties"));            LOG.info("env.properties loaded");        } catch (IOException e) {            // it's ok if the file is not there. we will just log that info.            LOG.info("didn't find env.properties in classpath so not loading it in the AppContextInitialized");        }    }}

You then need to add that initializer as a parameter to the ContextLoaderListener of spring as follows to your web.xml:

<context-param>    <param-name>contextInitializerClasses</param-name>    <param-value>somepackage.P13nApplicationContextInitializer</param-value></context-param><listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

You can also apply it to DispatcherServlet:

<servlet>    <servlet-name>dispatcherServlet</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>        <param-name>contextInitializerClasses</param-name>        <param-value>somepackage.P13nApplicationContextInitializer</param-value>    </init-param></servlet>


For some reason only one way works for me

public class ActiveProfileConfiguration implements ServletContextListener {       @Override    public void contextInitialized(ServletContextEvent sce) {        System.setProperty(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, "dev");        System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");    }

....

 <listener>     <listener-class>somepackahe.ActiveProfileConfiguration</listener-class> </listener> <listener>     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>