Main Tutorials

Spring MVC – How to set active profile

In this example, we will show you how to set active @Profile in a Spring MVC web application.

@Profile Examples

@Configuration
public class AppConfig {
 
	@Profile("dev")
	@Bean
	public CacheManager cacheManager() {
		//...
	}
 
	@Profile("live")
	@Bean
	public EhCacheManagerFactoryBean ehCacheCacheManager() {
		//...
	}
 
	@Profile("testdb")
	@Bean
	public DataSource dataSource() {
		//...
	}

}

To set active @Profile in Spring, define a value via spring.profiles.active system property.

1. web.xml

For normal web application, which contain web.xml file.

1.1 Set an active profile.

web.xml

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

1.2 Set multiple active profile.

web.xml

	<context-param>
	    <param-name>spring.profiles.active</param-name>
	    <param-value>dev, testdb</param-value>
	</context-param>

2. Servlet 3.0+ Container

For web application don’t have web.xml file, use one of the following methods:

2.1 Override onStartup

MyWebInitializer.java

package com.mkyong.servlet3;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public class MyWebInitializer extends
	AbstractAnnotationConfigDispatcherServletInitializer {

	//...

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		super.onStartup(servletContext);
		servletContext.setInitParameter("spring.profiles.active", "live");

		//Set multiple active profile
		//servletContext.setInitParameter("spring.profiles.active", "dev, testdb");
	}
	
}

2.2 Depends which context (root or servlet) to load the @Profile beans.

MyWebInitializer.java

package com.mkyong.servlet3;

import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class MyWebInitializer extends
	AbstractAnnotationConfigDispatcherServletInitializer {

	//If the @Profile beans are loaded via root context
	@Override
	protected WebApplicationContext createRootApplicationContext() {
		
		WebApplicationContext context = 
                     (WebApplicationContext)super.createRootApplicationContext();
	    	((ConfigurableEnvironment)context.getEnvironment()).setActiveProfiles("live");

		//Set multiple active profiles
		//((ConfigurableEnvironment)context.getEnvironment())
                //          .setActiveProfiles(new String[]{"live", "testdb"});

	    	return context;
	    
	}
	
	//If the @Profile beans are loaded via servlet context
	/*
	@Override
	protected WebApplicationContext createServletApplicationContext() {
		
		WebApplicationContext context = 
                     (WebApplicationContext)super.createServletApplicationContext();
	    	((ConfigurableEnvironment)context.getEnvironment()).setActiveProfiles("dev");
	    	return context;
	    
	}*/

}

References

  1. Spring @Profile Examples

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Daisie
2 years ago

Thank you! This helped me 🙂

Sanyukt
6 years ago

@mkyong
I have 2 environment for my application, which are a little different than the other. One of them has an extra table and that why an extra hbm file. So environment 1 has 10 hbm file and environment 2 has 11. I am able to set the active profile based on the environment. If there a way I can do some decision in my spring configuration file and include the extra hbm file ?
I am using XML based spring configuration file.

Sergii Poddyachiy
8 years ago

My application can’t see beans marked as belong to some profile in xml configuration file when I set profile via web.xml (spring.profiles.active). But if to set profile right before ApplicationContext initialization – everything works fine(System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, “lh”);).
What can be a problem?

Ashish Uniyal
8 years ago

Nice article