Spring Aspect(切面): 0 can‘t find referenced pointcut

注解方案配置切面,运行时报错 —— 找不到引用的切入点

Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut myPointCut

详细错误

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in file [C:\yc\workspaceSping\spring02\target\classes\com\yv\jdk\UserDaoImpl.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut myPointCut
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
	at com.yv.annotation.aspectj.Test.main(Test.java:10)
Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut myPointCut
	at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:315)
	at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:227)
	at org.springframework.aop.aspectj.AspectJExpressionPointcut.obtainPointcutExpression(AspectJExpressionPointcut.java:198)
	at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:177)
	at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:226)
	at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:289)
	at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:321)
	at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:128)
	at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:97)
	at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:78)
	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:347)
	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:299)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:430)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1798)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
	... 11 more

自定义切面

package com.yv.annotation.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 注册注解  切面 和 组件 的注解
 * @author 38929
 *
 */
@Aspect
@Component
public class MyAspect {
     

	@Pointcut("execution(* com.yv.jdk.*.*(..))")
	private void myPointCut() {
     
	}

	@Before("myPointCut()")
	public void myBefore(JoinPoint joinPoint) {
     
		System.out.println("前置通知:模拟执行权限检查。。。");
		System.out.println("目标类是:" + joinPoint.getTarget());
		System.out.println(",被植入增强处理的目标方法:" + joinPoint.getSignature().getName());
	}

	@AfterReturning("myPointCut()")
	public void myAfterReturning(JoinPoint joinPoint) {
     
		System.out.println("后置置通知:模拟记录日志。。。");
		System.out.println(",被植入增强处理的目标方法:" + joinPoint.getSignature().getName());
	} 

	@Around("myPointCut()")
	public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
     
		System.out.println("环绕开始,执行目标方法之前,模拟开启事务。。。"); 
		Object obj = proceedingJoinPoint.proceed(); 
		System.out.println("环绕结束,执行目标方法之后,模拟关闭事务");
		return obj;
	} 

	@AfterThrowing(value = "myPointCut()", throwing = "e")
	public void myAfterThrowing(JoinPoint joinPoint, Throwable e) {
     
		System.out.println("异常通知:出错了" + e.getMessage());
	} 

	@After("myPointCut()")
	public void myAfter() {
     
		System.out.println("最终通知:模拟方法介素后释放资源。。。");
	}

}

仔细检查 代码上无任何问题,由此怀疑因为aspectj版本不匹配导致

发现pom.xml中aspectj的版本过低

		<dependency>
			<groupId>aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.5.3</version>
		</dependency>

重置配置高版本的 —— 低版本注释用以警示

		<!-- 
		java.lang.IllegalArgumentException: 
		error at ::0 can't find referenced pointcut myPointCut
		
		<dependency>
			<groupId>aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.5.3</version>
		</dependency>
 		-->
 		
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.9.6</version>
			<scope>runtime</scope>
		</dependency>

运行结果正常

org.springframework.context.support.ClassPathXmlApplicationContext@439f5b3d, started on Tue Sep 08 16:44:33 CST 2020
环绕开始,执行目标方法之前,模拟开启事务。。。
前置通知:模拟执行权限检查。。。
目标类是:com.yv.jdk.UserDaoImpl@175c2241
,被植入增强处理的目标方法:addUser
添加用户
后置置通知:模拟记录日志。。。
,被植入增强处理的目标方法:addUser
最终通知:模拟方法介素后释放资源。。。
环绕结束,执行目标方法之后,模拟关闭事务

你可能感兴趣的:(Spring)