[英]SpringBoot - Unable to start embedded container
當我啟動 springboot 應用程序時,我的 SpringBootLoginController 類拋出此錯誤(無法啟動嵌入式容器),如下所示。這是一個 hello world 類型的 Spring Boot 應用程序示例。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.2.RELEASE)
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:536) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
我的 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.springboot</groupId>
<artifactId>HelloSpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>HelloSpringBoot</name>
<description>HelloSpringBoot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
我的控制器:
import org.springframework.boot.*;
import org.springframework.web.bind.annotation.*;
@RestController
public class SpringBootLoginController {
@RequestMapping("/hello")
String hello() {
return "Hello World!!!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootLoginController.class, args);
}
}
使用@SpringBootApplication 進行注釋可解決此問題。
@SpringBootApplication
@RestController
public class SpringBootLoginController {
@RequestMapping("/hello")
String hello() {
return "Hello World!!!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootLoginController.class, args);
}
}
或者,通過添加@EnableAutoConfiguration也可以解決此問題。
@EnableAutoConfiguration
@RestController
public class SpringBootLoginController {
@RequestMapping("/hello")
String hello() {
return "Hello World!!!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootLoginController.class, args);
}
}
嘗試使用@SpringBootApplication
注釋來注釋您的SpringBootLoginController
類。
@SpringBootApplication
@RestController
public class SpringBootLoginController {
@RequestMapping("/hello")
String hello() {
return "Hello World!!!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootLoginController.class, args);
}
}
就我而言,我正在使用 springboot 開發一個命令行項目。
@SpringBootApplication
public class Application implements CommandLineRunner {
//my code here
}
所以我只是使用簡單的啟動器。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
但是我也遇到了這個錯誤,並且我的 pom 中沒有真正連接的網絡相關依賴項。
最后我發現我的一個依賴項目在它自己的 pom 中使用了“javax.servlet.Servlet”。
如果你查看springboot的源代碼,它會在啟動應用程序時檢查你的項目中是否有任何“javax.servlet.Servlet”。 並在有任何“javax.servlet.Servlet”時嘗試啟動一個網絡“嵌入式容器”。
這就是我收到此錯誤的原因,因為我使用的是“spring-boot-starter”並且其中沒有 Web 容器。
所以解決方法很簡單,在“application.properties”中告訴springboot這不是web項目即可:
spring.main.web-environment=false
我刪除了我的 .m2 存儲庫,將我的版本從 1.3.1 更改為下面的 2.1.3,做了一個 Maven 清理,重新構建項目並第一次運行(使用 intellij)
parent>
<groupId>org.springframework.boot</groupId>
<version>2.1.3.RELEASE</version>
<artifactId>spring-boot-starter-parent</artifactId>
</parent>
將org.springframework.boot
的版本從 1.4.2 更改為 2.1.3
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
在我的情況下添加
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
解決的問題
您應該注釋您的SpringBootLoginController
類。 閱讀@SpringBootApplication
注釋。
@SpringBootApplication
@RestController
public class SpringBootLoginController {
@RequestMapping("/hello")
String hello() {
return "Hello World!!!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootLoginController.class, args);
}
}
如果您使用的是 maven,則通過構建
mvn package
而不是 IDE 構建 jar。
在我的情況下,IDE 構建的 jar 有很多問題。
直到現在我還沒有弄清楚這個問題,但我刪除了我的 .m2 存儲庫,提供了安裝版本,一切都像魅力一樣工作
你的依賴有問題。 添加這個
<dependency>
<groupId><groupId></groupId>
<artifactId><some dependency></artifactId>
<version><version></version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.