很久沒寫技術貼了,這兩天被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了。