Spring系列: 使用aop报错:nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$Refle


写了个最简单的aop例子
配置文件如下
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:c="http://www.springframework.org/schema/c"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:p="http://www.springframework.org/schema/p"
  8. xmlns:util="http://www.springframework.org/schema/util"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  11. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  12. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
  13. <aop:config>
  14. <aop:aspect ref="logger">
  15. <aop:pointcut id="dbmethod" expression="execution(* com.cet.peccore.DbInterfaceForPG.SetupTableForPG.*(..))" />
  16. <aop:before pointcut-ref="dbmethod" method="LogBefore" />
  17. <aop:after pointcut-ref="dbmethod" method="LogAfter" />
  18. </aop:aspect>
  19. </aop:config>
  20. <bean id="dbInterface" class="com.cet.peccore.DbInterface.DbInterface" autowire="byType"/>
  21. <bean id="setupTable" class="com.cet.peccore.DbInterfaceForPG.SetupTableForPG"/>
  22. <bean id="logger" class="com.cet.peccore.PecTrend.util.MyLogger"/>
  23. </beans>
但是运行的时候总是报如下错误:
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Cannot create inner bean '(inner bean)#29daf3' of type [org.springframework.aop.aspectj.AspectJMethodBeforeAdvice] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#29daf3': Cannot resolve reference to bean 'dbmethod' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbmethod': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Cannot create inner bean '(inner bean)#29daf3' of type [org.springframework.aop.aspectj.AspectJMethodBeforeAdvice] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#29daf3': Cannot resolve reference to bean 'dbmethod' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbmethod': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException  


原因:是由于classpath下缺少了aspectjweaver.jar的缘故。
解决办法:在pom.xml中加入依赖信息
Xml代码  收藏代码
  1. <dependency>  
  2.        <groupId> org.aspectj</groupId >  
  3.        <artifactId> aspectjweaver</artifactId >  
  4.        <version> 1.8.7</version >  
  5. </dependency>  
     




原文地址:https://www.cnblogs.com/strinkbug/p/5078002.html