Spring 循环依赖

Bean with name ‘xxxService’ has been injected into other beans [xxxService] 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.

用的spring版本是2.5.6,之前也有遇到过这种问题,确实是代码设计得比较耦 合,将相关逻辑拆分后得到解决。这两天这个问题,不管我如何拆解都如影随形,已经不是简单的A->B,B->A的循环引用了,而是深层次的逻 辑耦合,要解耦比较困难,说明在设计阶段有提高的余地。诡异的是,在融入另一个项目前是不会抛这个错误的,可见问题可能出在和新项目融合后的配置文件上, 简单分析了一下还是不得要领,于是开始启用google了。Juergen Hoeller说:

This is probably a consequence of the bean initialization order having changed, in combination with auto-proxying and maybe a pointcut that is too broad.

然后他给出了解决方案,有一个参数setAllowRawInjectionDespiteWrapping,默认是false,将其设成true即可。代码如下:

 
public class MyWebApplicationContext extends XmlWebApplicationContext { 
 
    @Override 
    protected DefaultListableBeanFactory createBeanFactory() { 
        DefaultListableBeanFactory beanFactory =  super.createBeanFactory(); 
        beanFactory.setAllowRawInjectionDespiteWrapping(true); 
        return beanFactory; 
    } 
 
}

 

然后在web.xml配置启用此context,

 

<context-param> 
        <param-name>contextClass</param-name> 
        <param-value>xxx.MyWebApplicationContext</param-value> 
</context-param>

 

 

然后就可以了。

当然这只是治标的办法,治本还得从设计的角度解决问题,尽量设计解耦的代码,有待提 高
posted @ 2017-02-27 11:41  roscee  阅读(734)  评论(0编辑  收藏  举报