实际上Spring是允许循环引用的存在的。
但是如果2个循环引用的对象 都是代理对象,就会出错。
比如2个对象都有@Async 或者@Transcationl注解,那么他们都会被代理增强,相互引用则会跑循环引用异常:
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘***’: Bean with name ‘***’ has been injected into other beans [***, ***]in its raw version as part of a circular reference,
but has eventually been wrapped (for example as part of auto-proxy creation). 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.
解决方法:1.循环引用本身就是设计有问题,调整方法位置
2.使用@Lazy注解,推迟Spring生产Bean的时间