在 Spring AOP 中,切入点表达式用于定义代理对象的方法
通配符:
*:匹配任意数量的字符,但不包括.。例如,com.example.* 将匹配 com.example 包下的所有类。..:匹配任意数量的字符,包括.。例如,com.example..* 将匹配 com.example 包及其子包下的所有类。类名和方法名:
com.example.MyClass.myMethod() 将匹配 com.example.MyClass 类中的 myMethod() 方法。参数匹配:
():匹配无参数的方法。(..):匹配任意数量和类型的参数。(String, int):匹配具有两个参数(一个字符串和一个整数)的方法。返回值匹配:
int com.example.MyClass.myMethod() 将匹配返回值为整数的 myMethod() 方法。注解匹配:
@annotation(com.example.MyAnnotation):匹配使用了 com.example.MyAnnotation 注解的方法。@within(com.example.MyAnnotation):匹配使用了 com.example.MyAnnotation 注解的类中的所有方法。切入点表达式组合:
&&:表示逻辑与,用于组合多个切入点表达式。例如,execution(* com.example.*.*(..)) && @annotation(com.example.MyAnnotation) 将匹配 com.example 包下使用了 com.example.MyAnnotation 注解的方法。||:表示逻辑或,用于组合多个切入点表达式。例如,execution(* com.example.MyClass.*(..)) || execution(* com.example.AnotherClass.*(..)) 将匹配 com.example.MyClass 和 com.example.AnotherClass 类中的所有方法。!:表示逻辑非,用于排除某些切入点表达式。例如,execution(* com.example.*.*(..)) && !execution(* com.example.MyClass.*(..)) 将匹配 com.example 包下的所有方法,但排除 com.example.MyClass 类中的方法。以下是一个简单的切入点表达式示例:
@Pointcut("execution(* com.example.MyClass.*(..))")
public void myPointcut() {}
这个切入点表达式将匹配 com.example.MyClass 类中的所有方法。