0

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?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • Checkout [this link](http://stackoverflow.com/questions/10041410/default-profile-in-spring-3-1/10041835#10041835) for more information. – Jdaydai Dec 27 '15 at 21:29
  • I don't know why your `getDefaultProfiles()` is showing `default` instead of `test`, but I do think you should be doing `rootContext.getEnvironment().acceptsProfiles("test")` instead of `ArrayUtils.contains(rootContext.getEnvironment().getActiveProfiles(), "test")`. – heenenee Dec 27 '15 at 21:34

1 Answers1

0

Did you add the:

  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

OR you can try do add it as init param for the DispatcherServlet, like this:

 <servlet>
      <servlet-name>dispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>spring.profiles.active</param-name>
          <param-value>test</param-value>
      </init-param>
  </servlet>