spring 使用aop 缺少依賴包aspectjweaver.jar 和spring-aop.jar報錯
執行後報了一大堆錯誤,挺嚇人的,不要怕,這寫錯誤可能是同一原因在不同地方重複出現;
錯誤太長,貼上幾段,分析一下
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'loginLogDao' defined in file [D:\actionSpring\chapter\chapter1\target\classes\com\smart\dao\LoginLogDao.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0': Cannot resolve reference to bean 'serviceMethod' while setting bean property 'pointcut'; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'serviceMethod': Failed to introspect bean class [org.springframework.aop.aspectj.AspectJExpressionPointcut
Caught exception while allowing TestExecutionListener [org.springframewor
Caused by: java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0':
Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
剛開始可能雲裡霧裡的,從上到下,分析一下黑體部分,發現了關鍵問題:j
springframework.aop.aspectj.AspectJExpression Pointcut for lookup method metadata: could not find class that it depends on
Pointcut 使用時,尋找方法時找不到依賴的類
java.lang.IllegalStateException: Failed to load ApplicationContext
發現問題出在配置檔案上,查一下applicationContect.xml且和aop:pointcut有關,發現:
<!-- 通過AOP配置提供事務增強,讓service包下所有Bean的所有方法擁有事務 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod"
expression=" execution(* com.smart.service..*(..))" />
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
</aop:config>
spring AOP是用aspectj來實現的,是依賴關係!
AspectJ是動態代理的一種實現!而spring預設使用的就是AspectJ來實現的動態代理
使用時需要依賴於一些第三方庫
java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
找到缺少的類包weaver
下載aspectjweaver.jar ,網上很多資源,附一個下載連結:
匯入該依賴包
有些人可能還缺少spring-aop.jar,這個一般使用IDE匯入spring框架會有。