關於Spring3.0.2中用annotation方式聲明@Pointcut時的報錯解決方案


前幾天在學習到了Spring3.0.2的AOP部分,當時教程上使用的是Spring2.5.0,然

後配置自定義的切入點函數(pointcut),在junit中測試正常通過,然後本人在

Spring3.0.2下測試,缺報錯,信息如下:


org.springframework.beans.factory.BeanCreationException: Error creating

bean with name 'userDAOImpl' defined in file [G:/JAVA/SPCCE

/Spring_1500_AOP_Annotation_avin/bin/com/bjsxt/dao/impl

/UserDAOImpl.class]: Initialization of bean failed; nested exception is

java.lang.IllegalArgumentException: error at ::0 can't find referenced

pointcut avinMethod
 at

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFac

tory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
 at
.....

 

主要是第一行中,顯示找不到自定義的切入點函數avinMethod,後來我以爲是配置

部分有誤,就直接把教程中的源代碼放到切面類(aspect)LogInterceprot中調用

! 結果依舊,對比了MyEclipse中的Spring框架與教程中的以後,覺得應該是jar

包的問題,同時也不排除框架本身的不兼容性,應爲無論是2.5.0還是3.0.2的

Spring都在AOP的時候用到了aspectjrt.jar  aspectjweaver.jar,由此思路,開

始尋找匹配的aspect組合,從csdn的論壇上看到一篇大同小異的文章鏈接如下:
http://fantasy0707.javaeye.com/category/101674?show_full=true

帖子中作者同樣自定義的@pointcut函數,框架爲Spring3.0.2,文中作者提到,要

使aspectj正常工作,還需要幾個其他的包,依次如下:
aspectjrt.jar
aspectjweaver.jar
aoplliance-1.0.jar
org.springframework.asm-3.0.2.RELEASE.jar
asm-all-3.2.jar
cglib-nodep-2.1.3.jar

Spring中的相關包:

commons-logging-1.1.1.jar
org.springframework.beans-3.0.2.REALEASE.jar
org.springframework.context-3.0.2.RELEASE.jar
org.springframework.context-support-3.0.2.RELEASE.jar
org.springframework.core-3.0.2-RELEASE.jar
org.springframework.expression-3.0.2-RELEASE.jar
org.springframework.aspects-3.0.2-RELEASE.jar
org.springframework.aop-3.0.2-RELEASE.jar

依帖子中的配置,集齊了jar包之後就開工了,結果很正常,報錯依舊!
用文中作者的配置,在測試@before正常,然後考慮是不是文中作者列舉的Jar包郵

版本更新,然後去google分別下載了各自最新的jar包,拼湊起來,還是報錯!
然後各交流羣求助,未果! 繼續沿着jar包的方向尋找解決方案……
後來在一個外國論壇上也看到了幾乎同樣的問題求助,不過下面的解決建議都是類

似換框架,就是將Spring3.0.2換成2.5.0 或者更改MyEclipse的編譯環境版本,均

試下,未果……
論壇地址:http://www.techinfopad.com/spring/100701686-cant-find-

referenced-pointcut-in-java-6.html


帶着問題,繼續學習Spring3.0.2的xml配置部分,好奇的將@pointcut用XML聲明,

結果測試通過! aspect class被調用,成功在目標函數之前引入邏輯,
這讓我看到了希望,至少XML還可以實現同樣的功能,接着是Spring3.0.2的

DataSource,其中用到了Jakarta commons的包,當時本子電量告罄,不得不來日

再戰,
翌日,在網上找jakarta commons的包,還不少,google的時候無意中發現了一個

下載jar包的網站,裏邊jar包相當齊全,不但順利下載玩jakarta commons的相關

包,還把前幾天很難找到的aspectjrt.jar  aspectjweaver.jar

aspectjtools.jar找到,順便也都下載了,然後整合到本子上又開始測試前幾天那

個錯誤! 因爲最開始的那篇csdn的帖子中,作者確實是實現了用annotation的方

式聲明並調用@pointcut自定義切入點函數:aspect method,所以希望總是存在!

今天把希望寄託在aspectjtools.jar上,因爲前幾次測試無論是換包,換代碼,都

沒有涉及到此包……


測試開始……

******************************************************
第一輪測試,Junit測試未通過,顯示缺少jar包,儘管沒通過,至少是給了提示,

如下:


org.springframework.beans.factory.BeanCreationException: Error creating

bean with name

'org.springframework.aop.config.internalAutoProxyCreator': Instantiation

of bean failed; nested exception is

org.springframework.beans.BeanInstantiationException: Could not

instantiate bean class

[org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoPr

oxyCreator]: Constructor threw exception; nested exception is

java.lang.NoClassDefFoundError:

org/aopalliance/intercept/MethodInterceptor
 at

........

第一行得知,缺少aoplliance.jar 導入,再測……
******************************************************************
提示缺少xerces.jar 轉機出現在這裏,在尋找xerces.jar的時候,看到一篇帖子

,上面大致意思是說,儘量使用Spring本身提供的jar包,這樣可以減少bao這件的

排斥性,即不兼容,想了想,也是,找到xerces.jar 導入,再測……

******************************************************************
未果……錯誤代碼沒有找到有用信息。

看來aspectjtools包並沒有起作用!
這時候想起了剛纔看到的建議,然後又進jar庫,無意間發現了一個

org.aspectj.aspectjweaver_1.5.3.jar的包,隨便把它加了進去,導入
項目,測試開始……
*********************************************************************

Junit測試進度條顯示綠色,測試通過,邏輯被調用,切入面工作正常……
日誌輸出如下:

2010-11-3 17:33:24

org.springframework.context.support.AbstractApplicationContext

prepareRefresh
信息: Refreshing

org.springframework.context.support.ClassPathXmlApplicationContext@182f0

db: startup date [Wed Nov 03 17:33:24 CST 2010]; root of context

hierarchy
2010-11-3 17:33:24

org.springframework.beans.factory.xml.XmlBeanDefinitionReader

loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-11-3 17:33:25

org.springframework.beans.factory.support.DefaultListableBeanFactory

preInstantiateSingletons
信息: Pre-instantiating singletons in

org.springframework.beans.factory.support.DefaultListableBeanFactory@1df

280b: defining beans

[org.springframework.context.annotation.internalConfigurationAnnotationP

rocessor,org.springframework.context.annotation.internalAutowiredAnnotat

ionProcessor,org.springframework.context.annotation.internalRequiredAnno

tationProcessor,org.springframework.context.annotation.internalCommonAnn

otationProcessor,logInterceptor,userDAOImpl,userService,org.springframew

ork.aop.config.internalAutoProxyCreator]; root of factory hierarchy
before method...Avin hello!
user saved!--This is DAOImpl1


**************************************************************
最後兩行的爲具體的輸出信息,最後一行是目標函數的輸出信息,倒數第二行是切

面中的邏輯! 幸運落在了最後的jar包上:org.aspectj.aspectjweaver.jar

經幾天測試得到的正確匹配Jar包列表如下:


org.springframework.beans-3.0.2.REALEASE.jar
org.springframework.context-3.0.2.RELEASE.jar
org.springframework.context-support-3.0.2.RELEASE.jar
org.springframework.core-3.0.2-RELEASE.jar
org.springframework.expression-3.0.2-RELEASE.jar
org.springframework.aspects-3.0.2-RELEASE.jar
org.springframework.aop-3.0.2-RELEASE.jar
commons-logging-1.1.1.jar

junit-4.9-SNAPSHOT-20100512-0041.jar
aspectjrt-111.jar
asm-all-3.2.jar
cglib-nodep-2.1.3.jar
aoplliance-1.0.jar
org.springframework.asm-3.0.2.RELEASE.jar
org.aspectj.aspectjweaver_1.5.3.jar


測試至此結束,問題解決……
希望能給遇到此問題的人一些思路……
***************************************

總結:
 1.j2ee中很多框架之間存在着錯綜複雜的jar包依賴關係(估計可以和

linux裏邊的rpm依賴媲美了……解決依賴關係的方法大多是尋找正確匹配的jar包

,這裏向大家提供跟兩個jar包的下載網站:

http://www.java2s.com/

http://www.findjar.com/jar/cglib/cglib-nodep/2.1_3/cglib-nodep-

2.1_3.jar.html

 2.CSDN很優秀,很多技術性的文章寫得很有針對性!學習交流的好地方!
 3.Google資料相較於Baidu多很多,尤其是外國技術資料!
 4.遇到報錯時,冷靜分析,找到可能的切入點,保持冷靜,清醒!
 5.當確定方向正確時,堅持下去!
 6.記住,豆芽長一房高,也是根菜!我們要鄙視敵人! 呵呵!

 

     好,關於這個問題就寫這麼多,希望

對大家有幫助……


同時也期待各位的相互交流指點……

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章