没有嵌入式servlet容器的Spring启动

时间:2014-08-11 14:35:01

标签: spring-boot

我有一个spring-boot网络应用程序,但我不想在嵌入式Tomcat / Jetty中启动它。禁用嵌入式容器的正确方法是什么?

如果我喜欢这样:

        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
           <exclusion>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-tomcat</artifactId>
           </exclusion>
        </exclusions>
    </dependency>

我一直在

org.springframework.context.ApplicationContextException: Unable to start embedded container; 

1 个答案:

答案 0 :(得分:23)

由于您使用的是Maven(而不是Gradle),请查看this指南和this部分官方文档。

基本步骤是:

使嵌入式servlet容器成为提供的依赖关系(因此将其从生成的战争中删除)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

添加应用程序初始化程序,如:

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

public class WebInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

}

为了能够引导Spring应用程序,需要该类,因为没有使用web.xml

相关问题