4

set active profile like context.getEnvironment().setActiveProfiles( "DEV" ); which can be achieved by using

public class SpringWebInitializer implements WebApplicationInitializer
{

    public void onStartup( final ServletContext servletContext ) throws ServletException
    {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.getEnvironment().setActiveProfiles("DEV" )

    }
}

But when extending AbstractAnnotationConfigDispatcherServletInitializer . how can we achieve setting active profile ?

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

}
Jens
  • 67,715
  • 15
  • 98
  • 113
arun kumar
  • 247
  • 2
  • 5
  • 15

4 Answers4

5

Activate your profile by using spring.profiles.active property.

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

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

}
alex
  • 8,904
  • 6
  • 49
  • 75
1

You have a few options..

  1. You can try having a context initializer to load the spring profile from a properties file on the classpath, like the following:

    public class ContextProfileInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(ContextProfileInitializer.class);
    
        private static final String DEFAULT_SPRING_PROFILE = "local";
    
        @Override
        public void initialize(final ConfigurableApplicationContext applicationContext) {
    
            ConfigurableEnvironment environment = applicationContext.getEnvironment();
            try {
                    environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:conf/application.properties"));
                    if (environment.getProperty("spring.profiles.active") == null) {
                        environment.setActiveProfiles(DEFAULT_SPRING_PROFILE);
                    }
                    LOGGER.info("Activated Spring Profile: " + environment.getProperty("spring.profiles.active"));
                } catch (IOException e) {
                    LOGGER.error("Could not find properties file in classpath.");
                }
       }
    
    }
    

Here are some guides with more info:

https://gist.github.com/rponte/3989915

http://www.java-allandsundry.com/2014/09/spring-webapplicationinitializer-and.html

  1. Alternatively (and a much easier way!) Use Spring Boot.

    You can simply define spring.profiles.active in an application.properties file in the classpath. This will automatically be picked up and loaded into your environment.

More info here:

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html

Pete
  • 1,500
  • 2
  • 16
  • 33
0

You can use @ActiveProfiles("DEV") on some of your @Configuration classes, but probably more useful would be passing profile from outside - just run your .jar with additional parameter like -Dspring.active.profiles=DEV

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
hi_my_name_is
  • 4,894
  • 3
  • 34
  • 50
  • thanks for the reply @ActiveProfiles("DEV") is in test cases.is there any other better way the java config. – arun kumar Jul 12 '16 at 07:20
0

I think it should be rather: -Dspring.profiles.active=... (Spring 4.1.5)

Zenek73
  • 1
  • 2