Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting "Active Profile" through Class AbstractAnnotationConfigDispatcherServletInitializer?

How would you set the "active profile" property when extending the class AbstractAnnotationConfigDispatcherServletInitializer ?

like image 334
Jamie White Avatar asked Dec 09 '13 14:12

Jamie White


1 Answers

Depending which contexts' profiles you want to set, one way to do it is to override the

AbstractAnnotationConfigDispatcherServletInitializer#createRootApplicationContext()

and

AbstractAnnotationConfigDispatcherServletInitializer#createServletApplicationContext()

to set the active profiles in there. For example

@Override
protected WebApplicationContext createRootApplicationContext() {
    WebApplicationContext context = (WebApplicationContext)super.createRootApplicationContext();
    ((ConfigurableEnvironment)context.getEnvironment()).setActiveProfiles("profiles");
    return context;
}

Note the super call. You'll want this so that the super implementation actually creates the WebApplicationContext from your @Configuration classes (or any context you specified).

like image 159
Sotirios Delimanolis Avatar answered Oct 31 '22 11:10

Sotirios Delimanolis