org.springframework.beans.factory.BeanCurrentlyInCreationException
昨天下午的时候,给公司的项目打了个版,发现一直报502错误了,最后在服务器日志上看了一下异常信息,发现报了以下异常信息,导致项目启动就报错了(pc:该项目在我电脑本地启动不报错,之前也没报错)。
错误代码如下:
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'voucherDynamicDao' : Bean with name 'voucherDynamicDao' has been injected into other beans [voucherServiceImpl] 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 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java: 622 ) ~[spring-beans- 5.1 . 7 .RELEASE.jar!/: 5.1 . 7 .RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java: 515 ) ~[spring-beans- 5.1 . 7 .RELEASE.jar!/: 5.1 . 7 .RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$ 0 (AbstractBeanFactory.java: 320 ) ~[spring-beans- 5.1 . 7 .RELEASE.jar!/: 5.1 . 7 .RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java: 222 ) ~[spring-beans- 5.1 . 7 .RELEASE.jar!/: 5.1 . 7 .RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java: 318 ) ~[spring-beans- 5.1 . 7 .RELEASE.jar!/: 5.1 . 7 .RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java: 199 ) ~[spring-beans- 5.1 . 7 .RELEASE.jar!/: 5.1 . 7 .RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java: 843 ) ~[spring-beans- 5.1 . 7 .RELEASE.jar!/: 5.1 . 7 .RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java: 877 ) ~[spring-context- 5.1 . 7 .RELEASE.jar!/: 5.1 . 7 .RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java: 549 ) ~[spring-context- 5.1 . 7 .RELEASE.jar!/: 5.1 . 7 .RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java: 142 ) ~[spring-boot- 2.1 . 5 .RELEASE.jar!/: 2.1 . 5 .RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java: 775 ) ~[spring-boot- 2.1 . 5 .RELEASE.jar!/: 2.1 . 5 .RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java: 397 ) ~[spring-boot- 2.1 . 5 .RELEASE.jar!/: 2.1 . 5 .RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java: 316 ) ~[spring-boot- 2.1 . 5 .RELEASE.jar!/: 2.1 . 5 .RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java: 1260 ) ~[spring-boot- 2.1 . 5 .RELEASE.jar!/: 2.1 . 5 .RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java: 1248 ) ~[spring-boot- 2.1 . 5 .RELEASE.jar!/: 2.1 . 5 .RELEASE] at cn.exrick.xboot.XbootApplication.main(XbootApplication.java: 58 ) ~[classes!/: 0.0 . 1 ] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 62 ) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 43 ) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java: 566 ) ~[na:na] at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java: 48 ) ~[xcr.dev.jar: 0.0 . 1 ] at org.springframework.boot.loader.Launcher.launch(Launcher.java: 87 ) ~[xcr.dev.jar: 0.0 . 1 ] at org.springframework.boot.loader.Launcher.launch(Launcher.java: 50 ) ~[xcr.dev.jar: 0.0 . 1 ] at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java: 51 ) ~[xcr.dev.jar: 0.0 . 1 ] |
异常分析:
BeanCurrentlyInCreationException是一个依赖闭环的问题,一般A依赖B,B依赖C,C依赖A容易导致这个问题,这样spring框架进行依赖注入创建bean的时候就容易发生异常。在本项目的代码中,则是因为VoucherDynamicDao注入了依赖voucherServiceImpl,而VoucherServiceImpl又注入了voucherDynamicDao。(PC:这是同事写的一段代码,很久很久都没动过,之前也没发生过异常,昨天突然出现了,因此告诫大家:编程需谨慎,务必符合规范)
解决方案:
原代码:
更正后的代码:
我们的编程习惯都是Service提供接口,ServiceImpl为Service接口的实现类,而@Service组件注解以及@Transactional事务注解都是加在ServiceImpl类上的,通常我们在Controller层或者其他层调用ServiceImpl里面的方法时,写的注入一般为service而不是serviceImpl,这样可以有效的防止出现依赖闭环问题,编程也更规范。
分类:
破尘/问题解决
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端