美文网首页
spingboot循环依赖报错

spingboot循环依赖报错

作者: 爱的旋转体 | 来源:发表于2020-02-14 13:30 被阅读0次

    springboot项目报以下循环依赖错误,报错的场景是:有三个类,A中注入B,B中注入C,C中注入A,其中B中有方法加了@Async。
    1.报错的原因:
    注入方法中加了@Async的对象早于AOP创建proxy类,当spring初始化applicationContext后,发现注入的对象并不是proxy类。
    2.解决办法有以下两种:
    ①在注入B的时候加上@Lazy;
    ②在B类上添加@Lazy;

    org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'asyncTaskService': Bean with name 'asyncTaskService' has been injected into other beans [userOrderService] 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:602)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:548)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
        at com.lhtc.jv.JvApp.main(JvApp.java:71)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
    

    参考:1.https://www.cnblogs.com/zjhgx/p/11047811.html
    2.https://stackoverflow.com/questions/28985144/spring-autowired-aop-circular-dependency

    相关文章

      网友评论

          本文标题:spingboot循环依赖报错

          本文链接:https://www.haomeiwen.com/subject/zusdfhtx.html