1.1 Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class pa...
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 }