Spring Boot 1.3.0 创建多个 ContextLoader 定义

Spring Boot 1.3.0 creates multiple ContextLoader definitions

我有一个使用 Spring Boot 1.3.0.RELEASE.

的应用程序

生产版本支持 运行 在 Tomcat 服务器 (AWS Elastic Beanstalk) 上。 大多数时候,当我部署应用程序时,我会收到错误消息:

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!

作为一个Spring启动应用程序,我什至没有web.xml

我的 ExampleApplication 看起来像:

@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);
    }
//...
}

我读到 servletContext.setInitParameter("contextConfigLocation", "<NONE>"); 可能有帮助,所以我添加了它,但它没有。 我认为排除 JerseyAutoConfiguration 会有帮助,但没有。

应用程序 运行 使用 spring-build:run maven 目标或直接从命令行 运行 安装包没有任何问题。

我的代码中没有“@EnableWebMVC”。 (即使我有,结果也是一样。)

听起来你在打this bug。您可以通过将 @Order(Ordered.HIGHEST_PRECEDENCE) 添加到 ExampleApplication 来解决此问题,以便它在 Jersey 的 SpringWebApplicationInitializer 之前运行,因此有机会将其关闭。

其实是我不好。我对 spring-boot-starter-jersey 有额外的依赖,它创建了自己的 ContextLoader。