Default profile in Spring 3.1

Solution 1:

Define your production environment as default profile in your web.xml

<context-param>
   <param-name>spring.profiles.default</param-name>
   <param-value>prod</param-value>
</context-param>

and if you want to use a different profile pass it as system property

mvn -Dspring.profiles.active="demo" jetty:run

Solution 2:

My experience is that using

@Profile("default")

the bean will only be added to the context if no other profile is identified. If you pass in a different profile, e.g. -Dspring.profiles.active="demo", this profile is ignored.

Solution 3:

I have the same issue, but I use WebApplicationInitializer in order to configure the ServletContext programmatically (Servlet 3.0+). So I do the following:

public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext sc) throws ServletException {
        // Create the 'root' Spring application context
        final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        // Default active profiles can be overridden by the environment variable 'SPRING_PROFILES_ACTIVE'
        rootContext.getEnvironment().setDefaultProfiles("prod");
        rootContext.register(AppConfig.class);

        // Manage the lifecycle of the root application context
        sc.addListener(new ContextLoaderListener(rootContext));
    }
}

Solution 4:

You may also consider removing the PROD profile, and use @Profile("!demo")