解決springboot服務(wù)啟動(dòng)報(bào)錯(cuò):Unable?to?start?embedded?contain
初次接觸spring-boot + spring-cloud構(gòu)建微服務(wù)項(xiàng)目,配置好項(xiàng)目后并選擇啟動(dòng)類啟動(dòng)時(shí)報(bào)如下錯(cuò)誤:
[main] ERROR org.springframework.boot.SpringApplication - Application startup failed
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)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:536)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at com.dispatchCenter.main.DispatchCenterApplication.main(DispatchCenterApplication.java:9)
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:189)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:162)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134)
... 8 common frames omitted
1. 根據(jù)報(bào)錯(cuò)信息發(fā)現(xiàn)是在刷新容器的方法onRefresh中拋出的
為什么是刷新容器的方法呢?相信大家都知道spring-boot是基于spring的擴(kuò)展,是簡(jiǎn)化spring配置的一個(gè)工具,
至于spring是怎么初始化的在本篇不做概述。我們?cè)谶@只需要知道
EmbeddedWebApplicationContext extends org.springframework.web.context.support.GenericWebApplicationContext
下面點(diǎn)進(jìn)去查看EmbeddedWebApplicationContext此類中刷新容器的方法的源碼如下:
?? ?protected void onRefresh() {
? ? ? ? super.onRefresh(); ?--這里就是刷新spring容器的入口
?
? ? ? ? try {
? ? ? ? ? ? this.createEmbeddedServletContainer(); ? --捕獲的是這個(gè)方法中的異常
? ? ? ? } catch (Throwable var2) {
? ? ? ? ? ? throw new ApplicationContextException("Unable to start embedded container", var2); --這里捕獲異常后拋出
? ? ? ? }
? ? }2. 接著被捕獲異常的方法源碼
private void createEmbeddedServletContainer() {
? ? ? ? EmbeddedServletContainer localContainer = this.embeddedServletContainer;
? ? ? ? ServletContext localServletContext = this.getServletContext();
? ? ? ? if (localContainer == null && localServletContext == null) {
? ? ? ? ? ? EmbeddedServletContainerFactory containerFactory = this.getEmbeddedServletContainerFactory(); ?--根據(jù)報(bào)錯(cuò)信息這里拋出的異常
? ? ? ? ? ? this.embeddedServletContainer = containerFactory.getEmbeddedServletContainer(new ServletContextInitializer[]{this.getSelfInitializer()});
? ? ? ? } else if (localServletContext != null) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? this.getSelfInitializer().onStartup(localServletContext);
? ? ? ? ? ? } catch (ServletException var4) {
? ? ? ? ? ? ? ? throw new ApplicationContextException("Cannot initialize servlet context", var4);
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? this.initPropertySources();
? ? }3. 再接著就是拋出異常的根源所在的源碼
通過(guò)查看源碼得出是啟動(dòng)時(shí)從beanFactory中找不到所需bean的存在,也就是bean根本沒(méi)有注冊(cè):
protected EmbeddedServletContainerFactory getEmbeddedServletContainerFactory() {
? ? ? ? String[] beanNames = this.getBeanFactory().getBeanNamesForType(EmbeddedServletContainerFactory.class); --這里通過(guò)類型去查詢bean,結(jié)果發(fā)現(xiàn)一個(gè)都沒(méi)有就拋出異常了,當(dāng)然如果超過(guò)一個(gè)也會(huì)拋出異常
? ? ? ? if (beanNames.length == 0) {
? ? ? ? ? ? throw new ApplicationContextException("Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.");
? ? ? ? } else if (beanNames.length > 1) {
? ? ? ? ? ? throw new ApplicationContextException("Unable to start EmbeddedWebApplicationContext due to multiple EmbeddedServletContainerFactory beans : " + StringUtils.arrayToCommaDelimitedString(beanNames));
? ? ? ? } else {
? ? ? ? ? ? return (EmbeddedServletContainerFactory)this.getBeanFactory().getBean(beanNames[0], EmbeddedServletContainerFactory.class);
? ? ? ? }
? ? }4. 知道原因了反過(guò)去查看代碼發(fā)現(xiàn)啟動(dòng)類中少寫了注解
太粗心大意了
@EnableEurekaServer @SpringBootApplication
5. 還有一種情況需要注意
我們使用注解標(biāo)注了這個(gè)啟動(dòng)類,但是還是提示Cannot resolve symbol *等問(wèn)題,一定要去檢查引包是否正確
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot中@Scheduled實(shí)現(xiàn)服務(wù)啟動(dòng)時(shí)執(zhí)行一次
- idea啟動(dòng)多個(gè)SpringBoot服務(wù)實(shí)例的最優(yōu)解決方法
- springboot項(xiàng)目同時(shí)啟動(dòng)web服務(wù)和grpc服務(wù)的方法
- centos7如何通過(guò)systemctl啟動(dòng)springboot服務(wù)代替java -jar方式啟動(dòng)
- IDEA中啟動(dòng)多個(gè)SpringBoot服務(wù)的實(shí)現(xiàn)示例
- springboot服務(wù)正常啟動(dòng)之后,訪問(wèn)服務(wù)url無(wú)響應(yīng)問(wèn)題及解決
- springboot項(xiàng)目如何在linux服務(wù)器上啟動(dòng)、停止腳本
- SpringBoot應(yīng)用剛啟動(dòng)時(shí)服務(wù)報(bào)大量超時(shí)的問(wèn)題及解決
相關(guān)文章
IntelliJ?IDEA教程之clean或者install?Maven項(xiàng)目的操作方法
這篇文章主要介紹了IntelliJ?IDEA教程之clean或者install?Maven項(xiàng)目的操作方法,本文分步驟給大家介紹兩種方式講解如何調(diào)試出窗口,需要的朋友可以參考下2023-04-04
Java進(jìn)階學(xué)習(xí):jar打包詳解
Java進(jìn)階學(xué)習(xí):jar打包詳解...2006-12-12
SpringBoot實(shí)現(xiàn)轉(zhuǎn)頁(yè)功能
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)轉(zhuǎn)頁(yè)功能,頁(yè)面的跳轉(zhuǎn)在web開(kāi)發(fā)中是經(jīng)常用的基礎(chǔ)功能,感興趣想要詳細(xì)了解可以閱讀下文,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值2023-05-05
比較Java數(shù)組和各種List的性能小結(jié)
這篇文章主要是分別對(duì)Java數(shù)組、ArrayList、LinkedList和Vector進(jìn)行隨機(jī)訪問(wèn)和迭代等操作,并比較這種集合的性能。有需要的可以參考借鑒。2016-08-08
Java設(shè)計(jì)模式之單例模式Singleton Pattern詳解
這篇文章主要介紹了Java設(shè)計(jì)模式之單例模式Singleton Pattern詳解,一些常用的工具類、線程池、緩存,數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)連接池、賬戶登錄系統(tǒng)、配置文件等程序中可能只允許我們創(chuàng)建一個(gè)對(duì)象,這就需要單例模式,需要的朋友可以參考下2023-12-12
Java生產(chǎn)者和消費(fèi)者例子_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
生產(chǎn)者-消費(fèi)者(producer-consumer)問(wèn)題,也稱作有界緩沖區(qū)(bounded-buffer)問(wèn)題,兩個(gè)進(jìn)程共享一個(gè)公共的固定大小的緩沖區(qū)。下文通過(guò)實(shí)例給大家介紹java生產(chǎn)者和消費(fèi)者,感興趣的朋友一起學(xué)習(xí)吧2017-05-05

