I have the following entry in web.xml:
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>test</param-value>
</context-param>
and following WebInitializer:
public class H2Starter implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
if (ArrayUtils.contains(rootContext.getEnvironment().getActiveProfiles(), "test")) {
System.out.println("test profile is active");
}
container.addListener(new ContextLoaderListener(rootContext));
}
}
When I start application with test profile (mvn clean install jetty:run
-Dspring.profiles.active=test) - execution goes into if statement and I see result in console.
If I start application without explicit profile (mvn clean install jetty:run
) - execution doesn't go into if statement. It is wrong from my side.
P.S. I tried to execute rootContext.getEnvironment().getDefaultProfiles()
but it returns single element array with value default
(should be test
according my opinion)
How to fix this?