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,这样可以有效的防止出现依赖闭环问题,编程也更规范。

 

posted @ 2020-05-26 14:59  帝莘  阅读(3562)  评论(0编辑  收藏  举报