Spring项目中用idea使用ProceedingJoinPoint类报错

本文记录了在IDEA中使用Spring AOP结合AspectJ时遇到的问题,即使用ProceedingJoinPoint时出现类未找到的错误。问题在于缺少必要的AspectJ相关依赖或依赖版本不匹配。解决方法为正确引入所需的AspectJ依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

idea使用ProceedingJoinPoint报错

Error:(18, 45) java: 无法访问org.aspectj.lang.ProceedingJoinPoint 找不到org.

信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@3fb6a447, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@79b4d0f, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6b2fad11, org.springframework.test.context.transaction.TransactionalTestExecutionListener@79698539, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@73f792cf]
九月 18, 2018 8:14:16 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring_aop/Spring-Config.xml]
九月 18, 2018 8:14:17 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@4439f31e: startup date [Tue Sep 18 20:14:17 GMT+08:00 2018]; root of context hierarchy
九月 18, 2018 8:14:17 下午 org.springframework.context.support.GenericApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [spring_aop/Spring-Config.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Cannot create inner bean '(inner bean)#7a52f2a2' 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)#7a52f2a2': Failed to introspect bean class [org.springframework.aop.aspectj.AspectJMethodBeforeAdvice] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/JoinPoint
九月 18, 2018 8:14:17 下午 org.springframework.test.context.TestContextManager prepareTestInstance
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@79b4d0f] to prepare test instance [spring_aop.Demo@527e5409]
java.lang.IllegalStateException: Failed to load ApplicationContext
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
	at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)

错误原因:使用idea创建的Spring项目自动导包中缺少一个jar包,或者导入到包版本不匹配,
解决方法:导入一个jar包即可,
点击进入下载地,我有注明具体是哪个包
点击链接


导入包之后问题解决:

### Spring AOP 中 `ProceedingJoinPoint` 的使用方法 #### 方法签名与继承关系 `ProceedingJoinPoint` 是 `JoinPoint` 接口的一个子接口,在其基础上增加了 `proceed()` 方法。此方法对于实现环绕通知至关重要,因为它允许控制目标方法的执行流程[^1]。 ```java public interface ProceedingJoinPoint extends JoinPoint { Object proceed() throws Throwable; } ``` #### 环绕通知的工作原理 通过 `@Around` 注解可以定义环绕通知逻辑。在该型的增强处理中,可以在调用实际业务逻辑前后加入额外的操作。具体来说: - 调用 `proceed()` 前的部分相当于前置通知; - `proceed()` 返回后的部分则对应于后置通知; 这使得开发者能够在不修改原有代码的情况下灵活地增加功能或改变行为模式。 #### 示例代码展示 下面是一个简单的例子来说明如何利用 `ProceedingJoinPoint` 实现环绕通知的功能: ```java import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @Aspect public class LoggingAspect { @Around("execution(* com.example.service..*(..))") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long start = System.currentTimeMillis(); try { // 执行被拦截的方法并返回结果 return joinPoint.proceed(); } finally { long elapsedTime = System.currentTimeMillis() - start; System.out.println(joinPoint.getSignature().getName() + " executed in " + elapsedTime + "ms"); } } } ``` 在此案例里,每当匹配到指定包下的任何公共成员函数被执行时,都会触发上述方面内的日志记录操作。它不仅会在每次调用前打印出开始时间戳,还会计算整个过程耗时并在完成后输出相关信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值