切点类型【PointCut】有哪些

在 Spring AOP 中,切点(Pointcut)定义了在哪些连接点(JoinPoint)上执行通知(Advice)。切点表达式的类型和用法多种多样,主要包括以下几种:

1. 基于方法签名的切点

这种切点类型最常用,用于匹配方法的执行。它可以根据方法的返回类型、类名、方法名和参数类型进行匹配。

  • execution():匹配方法执行的连接点。常用格式如下:
    • execution(public * *(..)):匹配所有公共方法。
    • execution(* com.example.service.*.*(..)):匹配 com.example.service 包中所有类的所有方法。
    • execution(* com.example.service.*.find*(..)):匹配 com.example.service 包中所有类的所有以 find 开头的方法。
    • execution(* com.example.service.*.*(String, ..)):匹配 com.example.service 包中所有类的第一个参数为 String 类型的方法。

2. 基于注解的切点

这种切点类型用于匹配带有特定注解的方法或类。

  • @annotation():匹配带有特定注解的方法。

    • @annotation(org.springframework.transaction.annotation.Transactional):匹配所有带有 @Transactional 注解的方法。
  • @within():匹配带有特定注解的类中的所有方法。

    • @within(org.springframework.stereotype.Service):匹配所有带有 @Service 注解的类中的方法。
  • @target():匹配目标对象的类带有特定注解的方法。

    • @target(org.springframework.stereotype.Service):匹配所有目标对象类带有 @Service 注解的方法。
  • @args():匹配传入参数带有特定注解的方法。

    • @args(com.example.annotation.Validated):匹配传入参数带有 @Validated 注解的方法。

3. 基于对象类型的切点

这种切点类型用于匹配特定对象类型的方法。

  • this():匹配当前 AOP 代理对象类型的方法。

    • this(com.example.service.MyService):匹配当前 AOP 代理对象是 MyService 类型的方法。
  • target():匹配目标对象类型的方法。

    • target(com.example.service.MyService):匹配目标对象是 MyService 类型的方法。

4. 基于参数的切点

这种切点类型用于匹配方法参数。

  • args():匹配传入参数是指定类型的方法。
    • args(java.io.Serializable):匹配传入参数是 Serializable 类型的方法。
    • args(String, int):匹配传入参数是 Stringint 类型的方法。

5. 其他切点类型

  • bean():匹配特定 Spring Bean 的方法。
    • bean(myBean):匹配名为 myBean 的 Spring Bean 的所有方法。

示例代码

以下是一个综合示例,展示了如何使用不同类型的切点:

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

    // 基于方法签名的切点
    @Before("execution(* com.example.service.*.*(..))")
    public void logMethodEntry(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        System.out.println("Method " + methodSignature.getName() + " called with args: " + Arrays.toString(joinPoint.getArgs()));
        System.out.println("Return type: " + methodSignature.getReturnType().getName());
    }

    // 基于注解的切点
    @AfterReturning(pointcut = "@annotation(org.springframework.transaction.annotation.Transactional)", returning = "result")
    public void logTransactionalMethod(JoinPoint joinPoint, Object result) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        System.out.println("Transactional method " + methodSignature.getName() + " returned: " + result);
    }
    
    // 基于对象类型的切点
    @Before("target(com.example.service.MyService)")
    public void logTargetServiceMethods(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        System.out.println("Target service method " + methodSignature.getName() + " called with args: " + Arrays.toString(joinPoint.getArgs()));
    }

    // 基于参数的切点
    @Before("args(java.io.Serializable)")
    public void logSerializableArgsMethods(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        System.out.println("Method " + methodSignature.getName() + " called with Serializable args: " + Arrays.toString(joinPoint.getArgs()));
    }
}

总结

在 Spring AOP 中,切点类型主要包括:

  1. 基于方法签名的切点:通过 execution() 表达式匹配方法。
  2. 基于注解的切点:通过 @annotation()@within()@target()@args() 表达式匹配带有特定注解的方法或类。
  3. 基于对象类型的切点:通过 this()target() 表达式匹配特定对象类型的方法。
  4. 基于参数的切点:通过 args() 表达式匹配方法参数类型。
  5. 其他切点类型:通过 bean() 表达式匹配特定 Spring Bean 的方法。

通过这些切点类型,可以灵活地在 Spring 应用中拦截和处理不同类型的方法调用。

  • 21
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值