今天学习Spring 的AOP,练习时,遇到 大概3个问题,都是因为在Spring的配置文件中,配置失败,导致Tomcat 无法正常启动,特在此总结一下。
问题一 报错提示:nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/JoinPoint
解决方法:
使用Spring AOP,要成功运行起代码,只用Spring提供给开发者的jar包是不够的,需要额外上网下载两个jar包:
2、aspectjweaver.jar
在此提供2个jar包的下载超链,大家点击包名即可跳转至度盘下载
PS:使用Maven做项目基本就不会遇到这坑了,在此感慨一句——Maven大发好!
———————————————————————————————————————————
问题二 报错提示:Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.dayuanIT.aop.HelloAOP' available: expected single matching bean but found 2: helloAOPImpl1,helloAOPImpl2
解决方法:出现这个问题是因为 Spring使用动态代理时,是基于接口的代理。注入的接口 有2个或多个实现类,则会报此异常。 这里我删掉helloAOPImpl1 ,helloAOPImpl2 中任意一个实现类即可 平复此异常。
———————————————————————————————————————————
问题三 报错提示:Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'helloAOPImpl1' is expected to be of type 'com.dayuanIT.aop.HelloAOPImpl1' but was actually of type 'com.sun.proxy.$Proxy14'
解决方法:在Spring的配置文件中,加入
<aop:aspectj-autoproxy proxy-target-class="true"/>
这段话,意为 强制使用cglib生成代理!那么基于类的代理将起作用 。
Spring使用动态代理或是CGLIB生成代理是有规则的, 如果proxy-target-class被设置为false或者这个属性或者这条配置语句被省略,那么基于接口的代理将起作用。 因此会导致被代理的类注入失败!