没有嵌入式 servlet 容器的 Spring Boot [英] Spring boot without embedded servlet container

查看:45
本文介绍了没有嵌入式 servlet 容器的 Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a spring-boot web application, but I don't want to start it in embedded Tomcat/Jetty. What is the correct way to disable embedded container?

如果我喜欢:

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

推荐答案

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

Since you are using Maven (instead of Gradle) check out this guide and this part of the official documentation.

基本步骤是:

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

Make the embedded servlet container a provided dependency (therefore removing it from the produced war)

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

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

Add an application initializer like:

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.

That class is needed in order to be able to bootstrap the Spring application since there is no web.xml used.

这篇关于没有嵌入式 servlet 容器的 Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆