网站建设与管理主要学什么,wordpress怎么加滑块,东莞常平镇房价多少,中铁建设集团有限公司网站Spring AOP 切点表达式#xff08;Pointcut Expression#xff09; 一、切点表达式概述
切点表达式 是 Spring AOP 用于定义哪些方法#xff08;或连接点#xff09;需要被拦截的规则#xff0c;主要基于 AspectJ 切点表达式语言。Spring AOP 仅支持方法级别的切点#…Spring AOP 切点表达式Pointcut Expression 一、切点表达式概述
切点表达式 是 Spring AOP 用于定义哪些方法或连接点需要被拦截的规则主要基于 AspectJ 切点表达式语言。Spring AOP 仅支持方法级别的切点相比 AspectJ 的字段、构造器等更广泛支持通过表达式指定目标方法结合通知Advice实现横切逻辑。
作用精确匹配需要应用切面逻辑的方法。使用场景日志记录、事务管理、权限检查等。表达式解析器Spring AOP 使用 AspectJ 的切点表达式解析器需要 spring-aspects 依赖。 二、切点表达式语法
AspectJ 切点表达式的通用语法为
execution(modifiers-pattern? return-type-pattern declaring-type-pattern? method-name-pattern(param-pattern) throws-pattern?)modifiers-pattern可选方法修饰符如 public、private。return-type-pattern方法返回值类型如 void、*任意类型。declaring-type-pattern可选声明方法的类或包如 com.example.service.*。method-name-pattern方法名称模式如 add*。param-pattern方法参数模式如 (…)任意参数。throws-pattern可选异常类型如 throws IOException。
通配符
*匹配任意字符用于方法名、类名、返回值等。…匹配任意数量的参数或包路径。匹配指定类及其子类。 三、常用切点表达式关键字
Spring AOP 主要支持以下 AspectJ 切点指示符Pointcut Designators
execution 匹配方法执行的连接点是 Spring AOP 最常用的切点指示符。示例 execution(* com.example.service.*.*(…))匹配 com.example.service 包下所有类的所有方法任意返回值任意参数。execution(public void com.example.UserService.add*(…))匹配 UserService 类中以 add 开头的公共方法返回值为 void。execution(* com.example…*.*(String, int))匹配 com.example 包及其子包下所有类的接收 String 和 int 参数的方法。 within 匹配指定类型或包内的所有方法。示例 within(com.example.service.*)匹配 com.example.service 包下所有类的所有方法。within(com.example…*)匹配 com.example 包及其子包下所有类的所有方法。 annotation 匹配带有特定注解的方法。示例 annotation(org.springframework.transaction.annotation.Transactional)匹配带有 Transactional 注解的方法。annotation(com.example.MyCustomAnnotation)匹配带有自定义注解的方法。 args 匹配方法参数类型。示例 args(String, …)匹配第一个参数为 String 的方法后面参数任意。args(String, int)匹配参数严格为 (String, int) 的方法。 this 匹配代理对象的类型通常是接口类型或类类型。示例 this(com.example.UserService)匹配代理对象是 UserService 类型的连接点。 target 匹配目标对象的类型。示例 target(com.example.UserService)匹配目标对象是 UserService 类型的连接点。 target 匹配目标对象带有特定注解的类型。示例 target(org.springframework.stereotype.Service)匹配带有 Service 注解的类中的方法。 args 匹配方法参数带有特定注解的场景。示例 args(com.example.MyAnnotation)匹配方法参数带有 MyAnnotation 注解的方法。 beanSpring 特有 匹配特定 Spring Bean 名称。示例 bean(userService)匹配 ID 为 userService 的 Bean 的所有方法。bean(*Service)匹配以 Service 结尾的 Bean 的所有方法。 四、切点表达式的组合
切点表达式可以通过逻辑运算符组合增强匹配灵活性
与两个条件都满足。 **示例execution(* com.example.service.*.*(…)) annotation(org.springframework.transaction.annotation.Transactional) ** 匹配 com.example.service 包下带有 Transactional 注解的方法。 ||或任一条件满足。 **示例**execution(* com.example.service.*.*(…)) || execution(* com.example.controller.*.*(…)) 匹配 service 或 controller 包下的方法。 !非取反。 **示例**execution(* com.example.service.*.*(…)) !execution(* com.example.service.UserService.*(…)) 匹配 service 包下除 UserService 类之外的所有方法。 五、常用切点表达式示例
以下是一些常见的切点表达式及其用途 匹配所有方法 java execution(* *.*(..))匹配所有类的所有方法任意返回值任意参数。 匹配特定包下所有方法 java execution(* com.example.service.*.*(..))匹配 com.example.service 包下所有类的所有方法。 匹配特定类的方法 java execution(* com.example.UserService.*(..))匹配 UserService 类中的所有方法。 匹配特定方法名前缀 java execution(* com.example.UserService.add*(..))匹配 UserService 类中以 add 开头的方法。 匹配特定返回值类型 java execution(String com.example.*.*(..))匹配 com.example 包下返回值类型为 String 的方法。 匹配特定参数类型 java execution(* com.example.*.*(String, int))匹配参数类型为 (String, int) 的方法。 匹配注解方法 java annotation(org.springframework.web.bind.annotation.GetMapping)匹配带有 GetMapping 注解的 Controller 方法。 匹配特定 Bean java bean(userService)匹配 ID 为 userService 的 Bean 的所有方法。 匹配子包 java within(com.example..*)匹配 com.example 包及其子包下所有类的所有方法。 六、切点表达式的配置方式
**1. **XML 配置
在 XML 中通过 aop:pointcut 定义切点
xml
aop:configaop:aspect idlogAspect refloggingAspectaop:pointcut idlogPointcut expressionexecution(* com.example.service.*.*(..))/aop:before pointcut-reflogPointcut methodlogBefore//aop:aspect
/aop:config**2. **注解配置
使用 Pointcut 注解定义切点
java
Aspect
Component
public class LoggingAspect {Pointcut(execution(* com.example.service.*.*(..)))public void logPointcut() {}Before(logPointcut())public void logBefore() {System.out.println(Before method execution);}
}Pointcut 方法通常为空仅用于定义切点供其他通知引用。确保启用 AOP 注解支持EnableAspectJAutoProxy**。** 七、注意事项
精确性 切点表达式应尽量精确避免匹配过多无关方法影响性能。例如execution(* *.*(…)) 过于宽泛可能导致不必要的代理开销。 Spring AOP 限制 仅支持方法级别的切点无法拦截字段访问或构造器。内部方法调用this.method()不会触发代理切面逻辑可能失效。 性能考虑 复杂的切点表达式或大量匹配方法可能增加解析和代理开销。使用 bean 或 within 限制范围以提高效率。 调试技巧 使用日志或调试工具验证切点匹配的方法。确保表达式语法正确错误表达式会导致匹配失败。 依赖 注解方式需要 spring-aspects 依赖 xml dependencygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactIdversion${spring.version}/version
/dependency八、总结
切点表达式 是 Spring AOP 的核心用于定义需要拦截的方法。基于 AspectJ 表达式语言Spring AOP 提供灵活的匹配规则常用关键字包括 execution、within、annotation 和 bean 等。通过 、||、! 组合表达式可以实现复杂的匹配逻辑。
核心要点
语法execution(modifiers? return-type declaring-type? method-name(param) throws?)。常用指示符 execution匹配方法签名。within匹配类或包。annotation匹配注解方法。bean匹配特定 Spring Bean。 组合方式使用 、||、! 实现复杂匹配。配置方式XMLaop:pointcut或注解Pointcut。注意事项确保表达式精确、注意 Spring AOP 的方法级别限制、优化性能。