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;
}
}
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");
}
}
You have a few options..
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
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:
您可以在某些@Configuration
类上使用@ActiveProfiles("DEV")
,但可能更有用的是从外部传递配置文件 - 只需使用-Dspring.active.profiles=DEV
等附加参数运行.jar
我认为应该是这样的:-Dspring.profiles.active = ...(Spring 4.1.5)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.