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

  1. <span style="font-size:18px">public class MyWebApplicationContext extends XmlWebApplicationContext {
  2.  
  3. @Override
  4. protected DefaultListableBeanFactory createBeanFactory() {
  5. DefaultListableBeanFactory beanFactory = super.createBeanFactory();
  6. beanFactory.setAllowRawInjectionDespiteWrapping(true);
  7. return beanFactory;
  8. }
  9.  
  10. }</span>

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

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

然后就可以了。

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

spring循环引用的问题的更多相关文章

  1. Spring 循环引用(一)一个循环依赖引发的 BUG

    Spring 循环引用(一)一个循环依赖引发的 BUG Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 循环 ...

  2. Spring 循环引用(二)源码分析

    Spring 循环引用(二)源码分析 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 循环引用相关文章: & ...

  3. Spring 循环引用(三)源码深入分析版

    @ 目录 前言 正文 分析 doGetBean 为什么Prototype不可以 createBean doCreateBean getEarlyBeanReference getSingleton b ...

  4. spring源码学习(三)--spring循环引用源码学习

    在spring中,是支持单实例bean的循环引用(循环依赖)的,循环依赖,简单而言,就是A类中注入了B类,B类中注入了A类,首先贴出我的代码示例 @Component public class Add ...

  5. spring 循环引用问题,在一次问题调试过程中发现有个小伙伴竟然把循环引用设置成false了。估计是百度的时候没小心额外的代码吧。。。

    循环引用属性操作: 1)AbstractAutowireCapableBeanFactory类中的allowCircularReferences被设置为了false. 2)代码: Annotation ...

  6. 【源码】spring循环引用

    spring在单例,非构造方法注入的情况下允许循环依赖 1.循环依赖 a引用b,b引用a.a创建的时候需要b,但是b没有创建,需要先去创建b,b创建的时候又没有a,这就出现的循环依赖的问题 2.为什么 ...

  7. Spring 循环引用(singleton与prototype初始化的区别)

    原文链接请参见:http://blog.csdn.net/u010723709/article/details/47185959

  8. spring jpa 实体互相引用返回restful数据循环引用报错的问题

    spring jpa 实体互相引用返回restful数据循环引用报错的问题 Java实体里两个对象有关联关系,互相引用,比如,在一对多的关联关系里 Problem对象,引用了标签列表ProblemLa ...

  9. Spring IOC 源码简单分析 03 - 循环引用

    ### 准备 ## 目标 了解 Spring 如何处理循环引用 ##测试代码 gordon.study.spring.ioc.IOC03_CircularReference.java   ioc03. ...

随机推荐

  1. Unity3D研究院之Editor下监听Transform变化

    美术有可以直接在Editor下操作Transform,我想去修正他们编辑的数值,所以我就得监听Transform.       C#   1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...

  2. ASP.NET会话(Session)保存模式--终于知道session为什么丢失了

    [原创]ASP.NET会话(Session)保存模式 作者:寒羽枫(cityhunter172) 大家好,已有四个多月没写东东啦.今日抽空就说一下 Session 在 .Net v1.0/v1.1 中 ...

  3. 026. asp.net中将图片以二进制方式保存到数据库并以HTTP流方式输出

    保存到数据库中 protected void imgbtnCreate_Click(object sender, ImageClickEventArgs e) { string PerHomeName ...

  4. 使用JavaScript根据从后台获取来的数据打开一个新的页面

    HTML代码是: 入库类型是: <select id="selectIn" onchange="goUrl(this.options[this.selectedIn ...

  5. Confluence 5.4实现与JIRA前所未有的集成

    http://www.blogjava.net/qileilove/archive/2014/03/03/410520.html 软件开发过程是一个讲求高度协作的过程,它需要很多不同领域团队的共同努力 ...

  6. WAMP Server助你在Windows上快速搭建PHP集成环境

    WAMP Server助你在Windows上快速搭建PHP集成环境 原文地址 我想只要爬过几天网的同学都会知道PHP吧,异次元的新版本就是基于PHP的WordPress程序制造出来的,还有国内绝大部分 ...

  7. .Net分布式缓存应用实例:Couchbase

    转自:http://www.cnblogs.com/wu-jian Couchbase概述 Couchbase最早叫Membase,是由Memcached项目组的一些头目另立的山头. 2011年与Co ...

  8. 【python】浅谈enumerate 函数

    enumerate 函数用于遍历序列中的元素以及它们的坐标: >>> for i,j in enumerate(('a','b','c')):  print i,j 0 a 1 b ...

  9. ARM汇编常用指令

    RAM汇编常用指令有MOV B BL LDR  STR

  10. SPOJ #442 Searching the Graph

    Just CS rookie practice on DFS\BFS. But details should be taken care of: 1. Ruby implementation got ...