首页 > 代码库 > Spring AOP常见错误解析

Spring AOP常见错误解析

Spring 3.0以后的版本相比于2时代的版本一个重要的变化就是aop所依赖的jar包没有一起发布,需要自己单独下载。

Spring AOP部分使用JDK动态代理或者CGLIB来为目标对象创建代理。如果被代理的目标实现了至少一个接口,则会使用JDK动态代理,如果用JDK动态代理,就必须为被代理的目标实现一个接口(要注意的地方是:需要将ctx.getBean()方法的返回值用接口类型接收);。所有该目标类型实现的接口都将被代理。若该目标对象没有实现任何接口,则创建一个CGLIB代理,如果使用CGLIB强制代理,就必选事先将CGLIB包导入项目,设置beanNameAutoProxyCreator的proxyTargetClass属性为true。

 

在编写aop程序时,常见的错误有以下几种:

1.如果提示如下错误:

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

   atorg.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412)

   atorg.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)

   atorg.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)

   atorg.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)

   atorg.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)

   atorg.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)

   atorg.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)

   atorg.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)

   atorg.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)

   atorg.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)

   atorg.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)

   atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)

   atorg.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)

   atorg.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)

   atcom.aoptest.com.Test.main(Test.java:14)

Caused by:java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice

   atjava.lang.ClassLoader.defineClass1(Native Method)

   atjava.lang.ClassLoader.defineClass(ClassLoader.java:800)

   atjava.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)

   atjava.net.URLClassLoader.defineClass(URLClassLoader.java:449)

   atjava.net.URLClassLoader.access$100(URLClassLoader.java:71)

   atjava.net.URLClassLoader$1.run(URLClassLoader.java:361)

   atjava.net.URLClassLoader$1.run(URLClassLoader.java:355)

   atjava.security.AccessController.doPrivileged(NativeMethod)

   atjava.net.URLClassLoader.findClass(URLClassLoader.java:354)

   atjava.lang.ClassLoader.loadClass(ClassLoader.java:425)

   atsun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)

   atjava.lang.ClassLoader.loadClass(ClassLoader.java:358)

   atjava.lang.ClassLoader.defineClass1(Native Method)

   atjava.lang.ClassLoader.defineClass(ClassLoader.java:800)

   atjava.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)

   atjava.net.URLClassLoader.defineClass(URLClassLoader.java:449)

   atjava.net.URLClassLoader.access$100(URLClassLoader.java:71)

   atjava.net.URLClassLoader$1.run(URLClassLoader.java:361)

   atjava.net.URLClassLoader$1.run(URLClassLoader.java:355)

   atjava.security.AccessController.doPrivileged(NativeMethod)

   atjava.net.URLClassLoader.findClass(URLClassLoader.java:354)

   atjava.lang.ClassLoader.loadClass(ClassLoader.java:425)

   atsun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)

   atjava.lang.ClassLoader.loadClass(ClassLoader.java:358)

   atjava.lang.ClassLoader.defineClass1(Native Method)

   atjava.lang.ClassLoader.defineClass(ClassLoader.java:800)

   atjava.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)

   atjava.net.URLClassLoader.defineClass(URLClassLoader.java:449)

   atjava.net.URLClassLoader.access$100(URLClassLoader.java:71)

   atjava.net.URLClassLoader$1.run(URLClassLoader.java:361)

   atjava.net.URLClassLoader$1.run(URLClassLoader.java:355)

   atjava.security.AccessController.doPrivileged(NativeMethod)

   atjava.net.URLClassLoader.findClass(URLClassLoader.java:354)

   atjava.lang.ClassLoader.loadClass(ClassLoader.java:425)

   atsun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)

   atjava.lang.ClassLoader.loadClass(ClassLoader.java:358)

   atorg.springframework.aop.config.ConfigBeanDefinitionParser.getAdviceClass(ConfigBeanDefinitionParser.java:410)

   atorg.springframework.aop.config.ConfigBeanDefinitionParser.createAdviceDefinition(ConfigBeanDefinitionParser.java:366)

   atorg.springframework.aop.config.ConfigBeanDefinitionParser.parseAdvice(ConfigBeanDefinitionParser.java:332)

   atorg.springframework.aop.config.ConfigBeanDefinitionParser.parseAspect(ConfigBeanDefinitionParser.java:227)

   atorg.springframework.aop.config.ConfigBeanDefinitionParser.parse(ConfigBeanDefinitionParser.java:115)

   atorg.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73)

   atorg.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1419)

   atorg.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1409)

   atorg.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:184)

   atorg.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:140)

   atorg.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:111)

   atorg.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)

   atorg.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)

   ...14 more

Caused by: java.lang.ClassNotFoundException: org.aopalliance.aop.Advice

   atjava.net.URLClassLoader$1.run(URLClassLoader.java:366)

   atjava.net.URLClassLoader$1.run(URLClassLoader.java:355)

   atjava.security.AccessController.doPrivileged(NativeMethod)

   atjava.net.URLClassLoader.findClass(URLClassLoader.java:354)

   atjava.lang.ClassLoader.loadClass(ClassLoader.java:425)

   atsun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)

   atjava.lang.ClassLoader.loadClass(ClassLoader.java:358)

   ...63 more

则说明缺少aop运行所依赖的jar包,需要下载,jar包为

aopalliance-1.0.jar

cglib-nodep-2.2.jar

com.springsource.org.aspectj.tools-1.6.6.RELEASE.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

commons-logging-1.1.3.jar

为了下载方面,我把这几个jar包共享出来,下载地址为:

2.提示以下错误:

Pre-instantiating singletons inorg.springframework.beans.factory.support.DefaultListableBeanFactory@72f08d6f:defining beans [persondaoImpl,transaction,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0];root of factory hierarchy

Exception in thread "main"java.lang.ClassCastException:com.sun.proxy.$Proxy0 cannot be cast to com.aoptest.com.PersonDaoImpl

   atcom.aoptest.com.Test.main(Test.java:15)

这是由于ctx.getBean()方法的返回值用接口的实现类接收,需要将ctx.getBean()方法的返回值改为用接口类型接收。

Spring AOP常见错误解析