【问题标题】:Spring boot without embedded servlet container没有嵌入式 servlet 容器的 Spring Boot
【发布时间】:2014-08-11 14:35:01
【问题描述】:

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

我一直在努力

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

【问题讨论】:

    标签: spring-boot


    【解决方案1】:

    由于您使用的是 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

    【讨论】:

    • return application.sources(Application.class) 可能应该是return application.sources(WebInitializer.class)(除非有另一个应用程序类)。 docs.spring.io/spring-boot/docs/current/reference/html/… 是一个很好的参考。该文档还将@SpringBootApplication 放在应用程序类中。
    • 感谢您的评论!该文档指的是最新版本。当我大约一年前添加答案时,我相当确定没有 @SpringBootApplication 注释。答案还假设存在一个用于引导 Spring Boot 的主要 Application
    猜你喜欢
    • 2017-09-20
    • 1970-01-01
    • 2017-11-17
    • 2017-05-02
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    相关资源
    最近更新 更多