芋道微服务 spring cloud 多模块,启动时无法依赖公共模块的bean

1、启动报错信息。

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-08-26 13:55:08.454 ERROR 5600 --- [main] [TID: N/A] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'operateLogApiImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'operateLogServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminUserService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'permissionServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'deptServiceImpl': Bean with name 'deptServiceImpl' has been injected into other beans [deptServiceImpl] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:332)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
    at cn.iocoder.yudao.module.system.SystemServerApplication.main(SystemServerApplication.java:25)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'operateLogServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminUserService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'permissionServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'deptServiceImpl': Bean with name 'deptServiceImpl' has been injected into other beans [deptServiceImpl] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.

2023-08-26 13:55:08.390  INFO 5600 --- [main] [TID: N/A] com.alibaba.druid.pool.DruidDataSource   : {dataSource-2} closing ...
2023-08-26 13:55:08.394  INFO 5600 --- [main] [TID: N/A] com.alibaba.druid.pool.DruidDataSource   : {dataSource-2} closed
2023-08-26 13:55:08.394  INFO 5600 --- [main] [TID: N/A] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} closing ...
2023-08-26 13:55:08.395  INFO 5600 --- [main] [TID: N/A] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} closed
2023-08-26 13:55:08.395  INFO 5600 --- [main] [TID: N/A] c.b.d.d.DynamicRoutingDataSource         : dynamic-datasource all closed success,bye
2023-08-26 13:55:08.397  INFO 5600 --- [main] [TID: N/A] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-08-26 13:55:08.418  INFO 5600 --- [main] [TID: N/A] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-08-26 13:55:08.454 ERROR 5600 --- [main] [TID: N/A] o.s.boot.SpringApplication               : Application run failed

2、项目环境与bug说明

  • 项目有一个微服务功能合并到另一个微服务里面去了,然后把这个微服务删除了,主pom里面对应的内容也删除了
  • 项目是用的芋道微服务架构,项目有公共模块,具体的业务微服务都会在pom文件中引用公共模块。
  • 项目之前项目启动都是正常的,所以在启动类上添加 扫描包路径也是没用的,因为已经在依赖里面添加了公共模块的依赖。

3、bug处理过程

  • 首先在代码的报错地方进行 debug,通过 evaluation 里面执行 SpringUtil.getApplicationContext().getBean("缺少的bean"),得到的结果就是null,说明公共依赖里的bean还没有创建。
  • 网上很多说要在启动类上加 @ComponentScan(basePackages = {"xxx.xxx"}),我试了没用,因为pom里面引用了,把公共模块所谓子模块引用就能获取到对应的bean了。
  • 然后联想到我只是把某一个子模块删除了,然后出现的这个问题,应该是maven依赖的问题,它可能直接去本地仓库去找公共模块了,然后去本地仓库一看,果然有对应的 artifactId;我就把本地仓库有关的依赖都给删除了。
  • 删除了启动还是报错,这就奇怪了,它应该没问题了啊,而且我已经重启过 idea,清理过缓存了,但是执行 maven clean 报错了,显示从仓库里找不到 公共依赖,我都删除了呀,怎么还去仓库里面找了??????
  • 最后的解决方法是 把 父 pom 里面的所有微服务注释掉,刷新maven,这时候各个微服务都没有父项目,然后再把注释打开,再刷新maven,然后启动就 ok 了。


    image.png

4、总结

这个bug困扰了我半天,最开始就怀疑是缓存的问题,我甚至都重启电脑了,还是启动报错,而且在 它的报错内容里面有很多 error create bean XXX ,然后看了很多无用的博客,启动了几个微服务报的错都不一样,每个服务报错的源头都是引用的 公用依赖的 bean 是null。所以就是公用模块的依赖问题了。
对于原来项目启动没问题,改动pom文件导致的项目启动失败,大概率都是maven本地仓库的缓存,或者就是项目里面的缓存。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容