【探索Spring底层】11.切点匹配

1. 前言

这里主要讲述一些切点匹配的相关例子,其主要是关于切点表达式匹配的实现。

主要有三种例子:

  • 给一个类中的其中一个方法增强
  • 根据注解给方法增强
  • Spring中@Transactional的底层实现

2. 给一个类中的其中一个方法增强

在使用AOP中使用注解会使用以下注解来实现对某个方法增强

@Pointcut("execution(* bar())")

那么用切点该怎么样实现呢?

假设有这样的一个类

static class T1 {
    public void foo() {
    }
    public void bar() {
    }
}

需要只对bar实现增强,那么就需要匹配到这个方法,再对其进行增强。

该怎么样匹配呢?这就要需要使用AspectJExpressionPointcut了

AspectJExpressionPointcut pt1 = new AspectJExpressionPointcut();
pt1.setExpression("execution(* bar())");
System.out.println(pt1.matches(T1.class.getMethod("foo"), T1.class));
System.out.println(pt1.matches(T1.class.getMethod("bar"), T1.class));

这里需要用到AspectJExpressionPointcut的matches方法

public boolean matches(Method method, Class<?> targetClass) {
    return this.matches(method, targetClass, false);
}

这个方法有两个参数:

  • Method method:需要判断是否匹配的方法
  • Class<?> targetClass:需要判断的类是哪个类,也就是目标类

运行结果:

image-20221213143910077

true代表匹配成功,false代表匹配失败


3. 根据注解给方法增强

根据注解给方法增强的步骤和给一个类中的其中一个方法增强步骤基本一样。

唯一不同的就是表达式的不同

AspectJExpressionPointcut pt2 = new AspectJExpressionPointcut();
pt2.setExpression("@annotation(org.springframework.transaction.annotation.Transactional)");
System.out.println(pt2.matches(T1.class.getMethod("foo"), T1.class));
System.out.println(pt2.matches(T1.class.getMethod("bar"), T1.class));

这里不过多解释


4. Spring中@Transactional的底层实现

虽然通过AspectJExpressionPointcut能实现根据注解给方法增强。

但是AspectJExpressionPointcut有个局限性就是只能匹配到方法上,但是@Transactional可以添加到类上、方法上等。

由此可见,Spring底层肯定不是用AspectJExpressionPointcut实现事务增强的

Spring底层是通过StaticMethodMatcherPointcut来实现事务增强的,具体代码如下

StaticMethodMatcherPointcut pt3 = new StaticMethodMatcherPointcut() {
    @Override
    public boolean matches(Method method, Class<?> targetClass) {
        // 检查方法上是否加了 Transactional 注解
        MergedAnnotations annotations = MergedAnnotations.from(method);
        if (annotations.isPresent(Transactional.class)) {
            return true;
        }
        // 查看类上是否加了 Transactional 注解
        annotations = MergedAnnotations.from(targetClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY);
        if (annotations.isPresent(Transactional.class)) {
            return true;
        }
        return false;
    }
};

System.out.println(pt3.matches(T1.class.getMethod("foo"), T1.class));
System.out.println(pt3.matches(T1.class.getMethod("bar"), T1.class));
System.out.println(pt3.matches(T2.class.getMethod("foo"), T2.class));
System.out.println(pt3.matches(T3.class.getMethod("foo"), T3.class));

这里面使用到了StaticMethodMatcherPointcut两种方法读取注解信息

static MergedAnnotations from(AnnotatedElement element) {
    return from(element, SearchStrategy.DIRECT);
}
static MergedAnnotations from(AnnotatedElement element, SearchStrategy searchStrategy) {
    return from(element, searchStrategy, RepeatableContainers.standardRepeatables());
}
  • AnnotatedElement element:需要在哪个元素上找注解信息,可以是方法也可以是类
  • SearchStrategy searchStrategy:搜索策略,默认只会找一层,也就是本类中,因为可能存在多层的情况,所以加在类上的时候要指定策略为MergedAnnotations.SearchStrategy.TYPE_HIERARCHY,这种类型会在继承树上找,不仅会搜索本类还会搜索父类和本类实现的接口等

然后通过annotations.isPresent(Transactional.class)检查注解信息中是否存在@Transactional注解

运行结果如下:

image-20221213145454870


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

起名方面没有灵感

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

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

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

打赏作者

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

抵扣说明:

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

余额充值