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.

Leave a Comment