spring aop開發常見錯誤


1.

1 Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [spring-aop.xml]; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice

解決方法:
下載引入 aopalliance-1.0.jar

使用spring aop 一般需要的依賴jar有: aopalliance-1.0.jar(AOP聯盟的API包,里面包含了針對面向切面的接口。 通常Spring等其它具備動態織入功能的框架依賴此包。) 、aspectjweaver-1.5.3.jar、javassist-3.9.0.GA.jar(Javassist的(JAVA編程助手)使Java字節碼操縱簡單。這是一個編輯Java字節碼的類庫)。

2.

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Instrumentalist' defined in class path resource [spring-aop.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting 'illegal identifier start (。)' at character position 52
execution(* com.luchao.springaop.Performer.perform(。。))

解決方法:在工具下。。顯示特別小,所有拿出來發現原來是。。的問題,把。。換成..就OK了。

3.

1 Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to com.luchao.springaop.Instrumentalist
2     at com.luchao.springaop.AspectTest.audienceShouldApplaud(AspectTest.java:13)
3     at com.luchao.springaop.AspectTest.main(AspectTest.java:19)

Spring的文檔中這么寫的:Spring AOP部分使用JDK動態代理或者CGLIB來為目標對象創建代理。如果被代理的目標實現了至少一個接口,則會使用JDK動態代理。所有該目標類型實現的接口都將被代理。若該目標對象沒有實現任何接口,則創建一個CGLIB代理。

所以,解決辦法是,如果用JDK動態代理,就必須為被代理的目標實現一個接口(要注意的地方是:需要將ctx.getBean()方法的返回值用接口類型接收);如果使用CGLIB強制代理,就必選事先將CGLIB包導入項目,設置beanNameAutoProxyCreator的proxyTargetClass屬性為true。

例如:

1 <aop:aspectj-autoproxy proxy-target-class="true"/>

解決方案:

1 public void audienceShouldApplaud() throws PerformanceException{
2   Performer performer = (Performer) applicationContext.getBean("Instrumentalist");
3   performer.perform();
4 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM