禁用嵌入式servlet加载

时间:2014-10-30 08:09:53

标签: rest spring-batch spring-boot

我有一个使用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.

2 个答案:

答案 0 :(得分:7)

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

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

答案 1 :(得分:0)

演出的后期但是.web(布尔值)现在已弃用,以支持:

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

请参阅WebApplicationType以获取其他枚举值

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