spring循环引用的问题

转载于:http://blog.xiaocaihua.com/archives/2010/10/problem-of-spring-circle-reference.html

 

 

很久没写技术贴了,这两天被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>

然后就可以了。

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

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值