解決springboot服務(wù)啟動報錯:Unable?to?start?embedded?contain
初次接觸spring-boot + spring-cloud構(gòu)建微服務(wù)項目,配置好項目后并選擇啟動類啟動時報如下錯誤:
[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ù)報錯信息發(fā)現(xiàn)是在刷新容器的方法onRefresh中拋出的
為什么是刷新容器的方法呢?相信大家都知道spring-boot是基于spring的擴展,是簡化spring配置的一個工具,
至于spring是怎么初始化的在本篇不做概述。我們在這只需要知道
EmbeddedWebApplicationContext extends org.springframework.web.context.support.GenericWebApplicationContext
下面點進去查看EmbeddedWebApplicationContext此類中刷新容器的方法的源碼如下:
?? ?protected void onRefresh() { ? ? ? ? super.onRefresh(); ?--這里就是刷新spring容器的入口 ? ? ? ? ? try { ? ? ? ? ? ? this.createEmbeddedServletContainer(); ? --捕獲的是這個方法中的異常 ? ? ? ? } 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ù)報錯信息這里拋出的異常 ? ? ? ? ? ? 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. 再接著就是拋出異常的根源所在的源碼
通過查看源碼得出是啟動時從beanFactory中找不到所需bean的存在,也就是bean根本沒有注冊:
protected EmbeddedServletContainerFactory getEmbeddedServletContainerFactory() { ? ? ? ? String[] beanNames = this.getBeanFactory().getBeanNamesForType(EmbeddedServletContainerFactory.class); --這里通過類型去查詢bean,結(jié)果發(fā)現(xiàn)一個都沒有就拋出異常了,當(dāng)然如果超過一個也會拋出異常 ? ? ? ? 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. 知道原因了反過去查看代碼發(fā)現(xiàn)啟動類中少寫了注解
太粗心大意了
@EnableEurekaServer @SpringBootApplication
5. 還有一種情況需要注意
我們使用注解標(biāo)注了這個啟動類,但是還是提示Cannot resolve symbol *等問題,一定要去檢查引包是否正確
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- SpringBoot中@Scheduled實現(xiàn)服務(wù)啟動時執(zhí)行一次
- idea啟動多個SpringBoot服務(wù)實例的最優(yōu)解決方法
- springboot項目同時啟動web服務(wù)和grpc服務(wù)的方法
- centos7如何通過systemctl啟動springboot服務(wù)代替java -jar方式啟動
- IDEA中啟動多個SpringBoot服務(wù)的實現(xiàn)示例
- springboot服務(wù)正常啟動之后,訪問服務(wù)url無響應(yīng)問題及解決
- springboot項目如何在linux服務(wù)器上啟動、停止腳本
- SpringBoot應(yīng)用剛啟動時服務(wù)報大量超時的問題及解決
相關(guān)文章
IntelliJ?IDEA教程之clean或者install?Maven項目的操作方法
這篇文章主要介紹了IntelliJ?IDEA教程之clean或者install?Maven項目的操作方法,本文分步驟給大家介紹兩種方式講解如何調(diào)試出窗口,需要的朋友可以參考下2023-04-04比較Java數(shù)組和各種List的性能小結(jié)
這篇文章主要是分別對Java數(shù)組、ArrayList、LinkedList和Vector進行隨機訪問和迭代等操作,并比較這種集合的性能。有需要的可以參考借鑒。2016-08-08Java設(shè)計模式之單例模式Singleton Pattern詳解
這篇文章主要介紹了Java設(shè)計模式之單例模式Singleton Pattern詳解,一些常用的工具類、線程池、緩存,數(shù)據(jù)庫,數(shù)據(jù)庫連接池、賬戶登錄系統(tǒng)、配置文件等程序中可能只允許我們創(chuàng)建一個對象,這就需要單例模式,需要的朋友可以參考下2023-12-12Java生產(chǎn)者和消費者例子_動力節(jié)點Java學(xué)院整理
生產(chǎn)者-消費者(producer-consumer)問題,也稱作有界緩沖區(qū)(bounded-buffer)問題,兩個進程共享一個公共的固定大小的緩沖區(qū)。下文通過實例給大家介紹java生產(chǎn)者和消費者,感興趣的朋友一起學(xué)習(xí)吧2017-05-05