解决springboot服务启动报错:Unable to start embedded contain

 更新时间:2022年08月18日 09:48:40   作者:Yeah-小海  
这篇文章主要介绍了解决springboot服务启动报错:Unable to start embedded contain的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

初次接触spring-boot + spring-cloud构建微服务项目,配置好项目后并选择启动类启动时报如下错误:

[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. 根据报错信息发现是在刷新容器的方法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();  --根据报错信息这里抛出的异常
            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,结果发现一个都没有就抛出异常了,当然如果超过一个也会抛出异常
        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. 知道原因了反过去查看代码发现启动类中少写了注解

太粗心大意了

@EnableEurekaServer
@SpringBootApplication

5. 还有一种情况需要注意

我们使用注解标注了这个启动类,但是还是提示Cannot resolve symbol *等问题,一定要去检查引包是否正确

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • springboot项目不同环境的配置读取方式

    springboot项目不同环境的配置读取方式

    SpringBoot支持application.properties、application.yml、application.yaml三种配置文件类型,可同时存在并合并配置,配置文件的读取优先级为:application.properties > application.yml > application.yaml,不同位置的相同类型配置文件
    2024-11-11
  • JavaGUI实现随机单词答题游戏

    JavaGUI实现随机单词答题游戏

    这篇文章主要为大家详细介绍了JavaGUI实现随机单词答题游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12
  • JAVA设计模式零基础解析之单例模式的八种方式

    JAVA设计模式零基础解析之单例模式的八种方式

    设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性
    2021-10-10
  • 解决Eclipse打开.java文件异常,提示用系统工具打开的问题

    解决Eclipse打开.java文件异常,提示用系统工具打开的问题

    这篇文章主要介绍了解决Eclipse打开.java文件异常,提示用系统工具打开的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • Springboot中使用Redis实现分布式锁的示例代码

    Springboot中使用Redis实现分布式锁的示例代码

    在分布式系统中,为了保证数据的一致性和任务的互斥执行,分布式锁是一种常见的解决方案,本文主要介绍了Springboot中使用Redis实现分布式锁的示例代码,具有一定的参考价值,感兴趣的可以了解一下
    2024-05-05
  • springboot Controller直接返回String类型带来的乱码问题及解决

    springboot Controller直接返回String类型带来的乱码问题及解决

    文章介绍了在Spring Boot中,当Controller直接返回String类型时可能出现的乱码问题,并提供了解决办法,通过在`application.yaml`中设置请求和响应的编码格式,并在自定义配置类中进行配置,可以有效解决这一问题
    2024-11-11
  • Java8中StringJoiner类的使用详解

    Java8中StringJoiner类的使用详解

    Java在java.util包中添加了一个新的最终类StringJoiner。可以用于构造由定界符分隔的字符序列。本文将通过示例和大家分享一下StringJoiner类的使用,需要的可以参考一下
    2022-10-10
  • Spring boot + thymeleaf 后端直接给onclick函数赋值的实现代码

    Spring boot + thymeleaf 后端直接给onclick函数赋值的实现代码

    这篇文章主要介绍了Spring boot + thymeleaf 后端直接给onclick函数赋值的实现代码,需要的朋友可以参考下
    2017-06-06
  • java 解压与压缩文件夹的实例详解

    java 解压与压缩文件夹的实例详解

    这篇文章主要介绍了 java 解压与压缩文件夹的实例详解的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,掌握这样的方法,需要的朋友可以参考下
    2017-10-10
  • Spring Boot中自动化配置的利弊以及解决方法

    Spring Boot中自动化配置的利弊以及解决方法

    这篇文章主要给大家介绍了关于Spring Boot中自动化配置的利弊以及解决方法,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring Boot具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-08-08

最新评论