zoukankan      html  css  js  c++  java
  • 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>

    然后就可以了。

    当然这只是治标的办法,治本还得从设计的角度解决问题,尽量设计解耦的代码,有待提 高
  • 相关阅读:
    函数即变量
    装饰器模型
    团队配合指令
    三元指令
    虚实之门
    for的逻辑
    我写的第4个程序(日志最近行读取函数)
    还在用WebBrowser吗?你out了!
    关于打印机共享的注意事项——又被叫去修电脑了
    MVVM转换器Int2StringConverter基础类
  • 原文地址:https://www.cnblogs.com/leonkobe/p/6473021.html
Copyright © 2011-2022 走看看