【问题标题】:Disable embedded servlet from loading禁止加载嵌入式 servlet
【发布时间】:2014-12-26 04:37:17
【问题描述】:

我有一个使用 Spring Boot 开发的批处理应用程序。我的批处理应用程序需要来自 spring-boot-starter-web 的依赖项。具体来说,我需要一个支持spring-hateoasjackson-databind 的REST 客户端。如何禁用嵌入式 Tomcat 启动?我需要使用哪些排除项?

@EnableAutoConfiguration(excludes = {
   /** What do I need to put here? */
})
@EnableBatchProcessing
@EnableConfigurationProperties
@ComponentScan
public class MyBatchApplication {
  public static void main(String... args) {
    SpringApplication.run(MyBatchApplication.class, args);
  }
}

至少,这些还不够,因为它以异常结束:

@EnableAutoConfiguration(exclude = {
  EmbeddedServletContainerAutoConfiguration.class,
  WebMvcAutoConfiguration.class,
  EmbeddedTomcat.class,
  DispatcherServletAutoConfiguration.class
})

例外是:

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

【问题讨论】:

    标签: rest spring-batch spring-boot


    【解决方案1】:

    您可以在创建应用程序时明确禁用 Web 支持。这意味着您不需要排除任何自动配置:

    @EnableAutoConfiguration
    @EnableBatchProcessing
    @EnableConfigurationProperties
    @ComponentScan
    public class MyBatchApplication {
        public static void main(String... args) {
            new SpringApplicationBuilder(MyBatchApplication.class).web(false).run(args);
        }
    }
    

    【讨论】:

    • 我已经得出了同样的结论,但是测试配置不正确仍然在启动 tomcat :)
    【解决方案2】:

    晚了,但 .web(boolean) 现在已弃用,取而代之的是:

    new SpringApplicationBuilder(MyApplication.class).web(WebApplicationType.NONE).run(args);

    有关其他枚举值,请参阅 WebApplicationType

    https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/WebApplicationType.html

    【讨论】:

      猜你喜欢
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 2023-03-11
      • 2020-02-02
      • 2010-11-11
      相关资源
      最近更新 更多