Spring AoP的切点匹配主要使用切点表达式进行匹配,下面是两种常用情况。
1.基于方法匹配
设置表达式,execution()括号里面使用正则表达式匹配注解所在的方法。
package com.example.springdemo.demos.a30;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.transaction.annotation.Transactional;
/**
* @author zhou
* @version 1.0
* @description TODO
* @date 2025/11/8 20:12
*/
public class a30 {
public static void main(String[] args) throws NoSuchMethodException {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("execution(* bar())");
System.out.println(pointcut.matches(T1.class.getMethod("foo"),T1.class));
System.out.println(pointcut.matches(T1.class.getMethod("bar"),T1.class));
}
static class T1{
@Transactional
public void foo(){
}
public void bar(){
}
}
}

2.基于注解匹配
java
AspectJExpressionPointcut pointcut1 = new AspectJExpressionPointcut();
pointcut1.setExpression("@annotation(org.springframework.transaction.annotation.Transactional)");
System.out.println(pointcut1.matches(T1.class.getMethod("foo"),T1.class));
System.out.println(pointcut1.matches(T1.class.getMethod("bar"),T1.class));
表达式的值为@annotation+注解所在的类路径。
