spring循环引用的问题

很久没写技术贴了,这两天被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即可。代码如下:

 

<span style="font-size:18px">public class MyWebApplicationContext extends XmlWebApplicationContext {  
  
    @Override  
    protected DefaultListableBeanFactory createBeanFactory() {  
        DefaultListableBeanFactory beanFactory =  super.createBeanFactory();  
        beanFactory.setAllowRawInjectionDespiteWrapping(true);  
        return beanFactory;  
    }  
  
}</span>

 

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

 

<span style="font-size:18px"><context-param>  
        <param-name>contextClass</param-name>  
        <param-value>xxx.MyWebApplicationContext</param-value>  
</context-param></span>

 

然后就可以了。

当然这只是治标的办法,治本还得从设计的角度解决问题,尽量设计解耦的代码,有待提 高,写下这篇文章是因为我在网上搜了很多,发现都不是很清楚,根据本文前面的出错信息提示也找不到比较好的答案。另一方面,随着spring越来越庞大, 各种参数纷繁复杂,只有开发人员才清楚每一个的含义和如何设置,已经不是当年追求简单的spring了。

 

posted on 2015-01-20 15:11  little fat  阅读(3106)  评论(0编辑  收藏  举报