endrec
endrec

Reputation: 467

Spring Boot 1.3.0 creates multiple ContextLoader definitions

I have an application which uses Spring Boot 1.3.0.RELEASE.

The production version is suppoed to run on a Tomcat server (AWS Elastic Beanstalk). Most of the time, when I deploy the application, I get an error:

18-Nov-2015 14:40:30.301 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log Spring WebApplicationInitializers detected on classpath: [org.glassfish.jersey.server.spring.SpringWebApplicationInitializer@16a15bdb, com.example.ExampleApplication@529635d6, org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration@19b5e8a2]
18-Nov-2015 14:40:37.851 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log Initializing Spring embedded WebApplicationContext
18-Nov-2015 14:40:49.148 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
 java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!

Being a Spring Boot application, I do not even have web.xml.

My ExampleApplication looks like:

@SpringBootApplication(exclude = JerseyAutoConfiguration.class)
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class ExampleApplication extends SpringBootServletInitializer {

    private static final Class<ExampleApplication> APPLICATION_CLASS = ExampleApplication.class;

    public static void main(final String[] args) {
        SpringApplication.run(APPLICATION_CLASS, args);
    }

@Override
    protected SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
        return application.sources(APPLICATION_CLASS);
    }

    @Override
    public void onStartup(final ServletContext servletContext) throws ServletException {
        servletContext.setInitParameter("contextConfigLocation", "<NONE>");
        super.onStartup(servletContext);
    }
//...
}

I have read that servletContext.setInitParameter("contextConfigLocation", "<NONE>"); might help, so I've added that, but it didn't. I thought excluding JerseyAutoConfiguration will help, but it didn't.

The applications runs without any issue using the spring-build:run maven goal, or running the package directly from the command line.

I do not have `@EnableWebMVC' anywhere in my code. (Even if I have, the result is the same.)

Upvotes: 3

Views: 1846

Answers (2)

endrec
endrec

Reputation: 467

Actually it was my bad. I had an extra dependency on spring-boot-starter-jersey, which created its own ContextLoader.

Upvotes: 0

Andy Wilkinson
Andy Wilkinson

Reputation: 116081

It sounds like you're hitting this bug. You can work around the problem by adding @Order(Ordered.HIGHEST_PRECEDENCE) to ExampleApplication so that it runs before Jersey's SpringWebApplicationInitializer and, therefore, has a chance to switch it off.

Upvotes: 1

Related Questions