spring aop Pointcut execution规则

本文详细解析了AOP(面向切面编程)中execution表达式的使用方法,包括任意公共方法、特定前缀方法、特定接口及类方法的执行场景,适用于理解和应用Spring AOP。

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

任意公共方法的执行:
execution(public * *(..))
##public可以省略, 第一个* 代表方法的任意返回值 第二个参数代表任意包+类+方法 (..)任意参数

任何一个以“set”开始的方法的执行:
execution(* set*(..))

UserService接口的任意方法:
execution(* com.coffee.service.UserService.*(..))

定义在com.coffee.service包里的任意方法的执行:
execution(* com.coffee.service.*.*(..))
#第一个 .* 代表任意类, 第二个 .* 代表人以方法

定义在service包和所有子包里的任意类的任意方法的执行:
execution(* com.coffee.service..*.*(..))
# ..* 代表任意包或者子包

定义在com.coffee包和所有子包里的UserService类的任意方法的执行:
execution(* com.coffee..UserService.*(..))")
--------------------- 
作者:-droidcoffee- 
来源:优快云 
原文:https://blog.youkuaiyun.com/id19870510/article/details/79163639 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

### Spring AOP Execution Expression Usage and Examples In Spring AOP, the `execution` pointcut designator allows specifying which method executions should be matched based on patterns that describe methods' signatures. This powerful feature enables precise control over where aspects apply within an application. The general syntax for defining execution expressions follows this pattern: ```java execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?) ``` For practical purposes, most developers focus primarily on matching return type (`ret-type-pattern`), package/class (`declaring-type-pattern`) and method names along with parameters (`name-pattern(param-pattern)`). #### Common Patterns Used in Execution Expressions - **Matching all public methods running anywhere**: ```java execution(public * *(..)) ``` - **Methods returning void from any class under com.example.service package**: ```java execution(void com.example.service.*.*(..)) ``` - **Specific Method Name Across All Classes Under Package** To match only specific named methods across classes inside a particular package hierarchy: ```java execution(* get*(..)) ``` This matches every method starting with "get" regardless of parameter count or types as long as it resides somewhere beneath the specified path. To demonstrate how these rules work together, consider creating an aspect logging entry/exit points around service layer operations using annotations like so[^1]: ```java @Aspect public class LoggingAspect { @Before("execution(* com.example.services..*.*(..))") public void logMethodEntry(JoinPoint joinPoint){ System.out.println("Entering:" + joinPoint.getSignature().getName()); } } ``` Here, whenever any method defined by services located directly under or nested within `com.example.services`, including subclasses, gets invoked—the associated message will print before proceeding further into actual business logic implementation details. --related questions-- 1. How does one define multiple pointcuts combining different criteria? 2. What differences exist between Before, After Returning, Around advices concerning their typical applications scenarios? 3. Can you provide more complex examples involving conditional weaving through custom annotations alongside standard ones provided out-of-the-box by Spring AOP framework? 4. In what situations might choosing XML configuration over annotation-based approach prove beneficial when working extensively with cross-cutting concerns management via AOP techniques offered by Spring ecosystem tools?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值