execution切点函数

execution函数用于匹配方法执行的连接点,语法:

execution(修饰符匹配?返回值类型 方法名称匹配(参数匹配) 抛出异常匹配)

事例:

"execution(* cn.libin..*.*(..)))"

   “*”表示任意返回类型

     ”cn.libin..“表示cn.libin包中的任意子包

    “*.*”表示子包中的任意类的任意方法

    “(..)”表示匹配任意多个参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AOP(面向切面编程)是一种编程范式,它允许开发人员在不改变原有代码的情况下,动态地横向扩展代码功能。AOP的核心是切点(Pointcut)、切面(Aspect)、通知(Advice)和织入(Weaving)。 其中,切点是AOP中最重要的概念之一。切点定义了在哪些地方进行切面的织入操作。切点表达式可以使用execution函数来定义,其语法如下: ``` execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?) ``` 其中,各参数的含义如下: - modifiers-pattern:方法修饰符,如public、protected、private等。 - ret-type-pattern:返回值类型,如int、String等。 - declaring-type-pattern:声明类型,如com.example.*Service、\*Service等。 - name-pattern:方法名,如add*、get*等。 - param-pattern:参数类型,如java.lang.String、com.example.*等。 - throws-pattern:异常类型,如java.io.IOException、java.lang.Exception等。 举个例子,以下是一个切点表达式的示例: ``` execution(* com.example.service.*.*(..)) ``` 该表达式表示,切点匹配com.example.service中所有类的所有方法。 在Spring中,可以使用@Pointcut注解来定义切点。例如: ``` @Pointcut("execution(* com.example.service.*.*(..))") public void servicePointcut() {} ``` 定义好切点后,就可以在切面类中使用该切点了。例如: ``` @Aspect public class LoggingAspect { @Before("com.example.aspect.LoggingAspect.servicePointcut()") public void logBefore(JoinPoint joinPoint) { // do something before method execution } } ``` 上述代码中,@Before注解表示在方法执行前执行通知,括号中的字符串表示切点表达式,指定了在servicePointcut切点匹配的方法执行前执行通知。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值