gpt4 book ai didi

spring-boot - 没有嵌入式 servlet 容器的 Spring Boot

转载 作者:行者123 更新时间:2023-12-03 12:45:15 25 4
gpt4 key购买 nike

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

最佳答案

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

基本步骤是:

使嵌入的 servlet 容器成为 提供依赖(因此从产生的 war 中删除它)

<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。用过的。

关于spring-boot - 没有嵌入式 servlet 容器的 Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25245834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com