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

and*_*w.z 19 spring-boot

我有一个spring-boot Web应用程序,但我不想在嵌入式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>
Run Code Online (Sandbox Code Playgroud)

我一直在

org.springframework.context.ApplicationContextException: Unable to start embedded container; 
Run Code Online (Sandbox Code Playgroud)

geo*_*and 24

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

基本步骤是:

使嵌入式servlet容器成为提供的依赖项(从而将其从生成的war中删除)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

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

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

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