关于 spring aop execution 属性的使用

本文详细解析了AOP(面向切面编程)中的切入点表达式语法,包括执行(execution)、全限定类型名匹配、参数模式等,并通过多个实例展示了如何使用这些表达式来精确指定切点。

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

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
除 了返回类型模式(上面代码片断中的ret-type-pattern),名字模式和参数模式以外,所有的部分都是可选的。 返回类型模式决定了方法的返回类型必须依次匹配一个连接点。 你会使用的最频繁的返回类型模式是 *,它代表了匹配任意的返回类型。 一个全称限定的类型名将只会匹配返回给定类型的方法。名字模式匹配的是方法名。 你可以使用 * 通配符作为所有或者部分命名模式。 参数模式稍微有点复杂:() 匹配了一个不接受任何参数的方法, 而 (..) 匹配了一个接受任意数量参数的方法(零或者更多)。 模式 (*) 匹配了一个接受一个任何类型的参数的方法。 模式 (*,String) 匹配了一个接受两个参数的方法,第一个可以是任意类型,第二个则必须是String类型。

下面给出一些常见切入点表达式的例子。

任意公共方法的执行:
execution(public * *(..))
任何一个以“set”开始的方法的执行:
execution(* set*(..))
AccountService 接口的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
定义在service包里的任意方法的执行:
execution(* com.xyz.service.*.*(..))
定义在service包或者子包里的任意方法的执行:
execution(* com.xyz.service..*.*(..))
在service包里的任意连接点(在Spring AOP中只是方法执行) :
within(com.xyz.service.*)
在service包或者子包里的任意连接点(在Spring AOP中只是方法执行) :
within(com.xyz.service..*)
实现了 AccountService 接口的代理对象的任意连接点(在Spring AOP中只是方法执行) :
this(com.xyz.service.AccountService)
'this'在binding form中用的更多:- 请常见以下讨论通知的章节中关于如何使得代理对象可以在通知体内访问到的部分。
实现了 AccountService 接口的目标对象的任意连接点(在Spring AOP中只是方法执行) :
target(com.xyz.service.AccountService)
'target'在binding form中用的更多:- 请常见以下讨论通知的章节中关于如何使得目标对象可以在通知体内访问到的部分。
任何一个只接受一个参数,且在运行时传入的参数实现了 Serializable 接口的连接点 (在Spring AOP中只是方法执行)
args(java.io.Serializable)
'args' 在binding form中用的更多:- 请常见以下讨论通知的章节中关于如何使得方法参数可以在通知体内访问到的部分。 请注意在例子中给出的切入点不同于 execution(* *(java.io.Serializable)): args只有在动态运行时候传入参数是可序列化的(Serializable)才匹配,而execution 在传入参数的签名声明的类型实现了 Serializable 接口时候匹配。
有一个 @Transactional 注解的目标对象中的任意连接点(在Spring AOP中只是方法执行)
@target(org.springframework.transaction.annotation.Transactional)
'@target' 也可以在binding form中使用:请常见以下讨论通知的章节中关于如何使得annotation对象可以在通知体内访问到的部分。
任何一个目标对象声明的类型有一个 @Transactional 注解的连接点(在Spring AOP中只是方法执行)
@within(org.springframework.transaction.annotation.Transactional)
'@within'也可以在binding form中使用:- 请常见以下讨论通知的章节中关于如何使得annotation对象可以在通知体内访问到的部分。
任何一个执行的方法有一个 @Transactional annotation的连接点(在Spring AOP中只是方法执行)
@annotation(org.springframework.transaction.annotation.Transactional)
'@annotation' 也可以在binding form中使用:- 请常见以下讨论通知的章节中关于如何使得annotation对象可以在通知体内访问到的部分。
任何一个接受一个参数,并且传入的参数在运行时的类型实现了 @Classified annotation的连接点(在Spring AOP中只是方法执行)
@args(com.xyz.security.Classified)

本文来自优快云博客:http://blog.youkuaiyun.com/terryzero/archive/2009/04/27/4128858.aspx

### 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
发出的红包

打赏作者

BUG弄潮儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值