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] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
九月 19, 2018 11:43:19 上午 org.springframework.test.context.TestContextManager prepareTestInstance
严重:

Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@bc05a6] to prepare test instance [com.smart.service.UserServiceTest@729b1e]
java.lang.IllegalStateException: Failed to load ApplicationContext

 

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>

使用时需要依赖于一些第三方库

java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException

找到缺少的类包weaver

下载aspectjweaver.jar ,网上很多资源,附一个下载链接

导入该依赖包

有些人可能还缺少spring-aop.jar,这个一般使用IDE导入spring框架会有。

另附:spring官方文档

### 回答1: 使用Spring AOP需要依赖以下jar: 1. spring-aop: 核心AOP,提供了AOP的基本功能。 2. aspectjweaver: AspectJ是一个基于Java的面向切面编程框架,Spring AOP通过aspectjweaverAspectJ集成。 3. spring-beans: 提供了Spring中的Bean管理功能。 4. spring-context: 提供了上下文(context)相关的功能,括对AOP的支持。 此外,你还需要引入Spring核心spring-core)和Spring上下文(spring-context),以支持Spring AOP的基本功能。 ### 回答2: 使用Spring AOP需要依赖以下几个核心的jar: 1. spring-core.jar:这是Spring框架的核心含了Spring中的基本功能和核心类。 2. spring-aop.jar:这个jar含了Spring AOP模块的实现代码。 3. spring-beans.jar:这个jar含了Spring中的Bean的相关功能和类。 4. aspectjrt.jar:这是AspectJ的运行时库,Spring AOP实际上是对AspectJ AOP实现的封装,因此需要使用AspectJ的运行时库。 5. aspectjweaver.jar:这是AspectJ的织入器,用于在运行时将切面织入到目标对象中。 除了以上核心的jar外,还可能需要依赖其他的jar,具体情况取决于你在使用Spring AOP时用到的功能和组件。例如,如果你使用Spring的事务管理功能,可能还需要依赖spring-tx.jar;如果你使用Spring的注解功能,可能还需要依赖spring-context.jar等。 总之,使用Spring AOP需要依赖jar主要spring-core.jarspring-aop.jarspring-beans.jaraspectjrt.jaraspectjweaver.jar,同时还可能需要依赖其他的相关jar
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值