Spring系列之AOP分析之为目标类挑选合适的Advisor(五)

简介:

我们在之前的文章中分析了Advisor的生成过程以及在Advisor中生成Advise的过程。在这一篇文章中我们说一下为目标类挑选合适的Advisor的过程。通过之前的分析我们知道,一个切面类可以生成多个Advisor(多个切面类的话那就更多多的Advisor了),这些Advisor是否都能适用于我们的目标类呢?这就需要通过Advisor中所拥有的Pointcut来进行判断了。先回到我们最开始的例子:

//手工创建一个实例
AspectJService aspectJService = new AspectJServiceImpl();
//使用AspectJ语法 自动创建代理对象
AspectJProxyFactory aspectJProxyFactory = new AspectJProxyFactory(aspectJService);

我们在我们的AspectJProxyFactory中传入了我们的目标对象。我们再回到AspectJProxyFactory的addAdvisorsFromAspectInstanceFactory方法中。

    private void addAdvisorsFromAspectInstanceFactory(MetadataAwareAspectInstanceFactory instanceFactory) {
        //获取Advisor的过程我们在之前分析了
        List<Advisor> advisors = this.aspectFactory.getAdvisors(instanceFactory);
        //这句代码的意思是为我们的目标类挑选合适的Advisor也是我们这一次要分析的内容
        advisors = AopUtils.findAdvisorsThatCanApply(advisors, getTargetClass());
        AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(advisors);
        //为Advisor进行排序
        AnnotationAwareOrderComparator.sort(advisors);
        addAdvisors(advisors);
    }

在AspectJProxyFactory中是通过调用AopUtils中的findAdvisorsThatCanApply方法来为目标类挑选合适的Advisor的或者是进判断哪些Advisor可以作用于目标类。在这个方法中传入了两个参数,一个参数是Advisor的集合,一个参数是目标类Class。我们看一下getTargetClass()这个方法的内容:
AdvisedSupport#getTargetClass

    @Override
    public Class<?> getTargetClass() {
        //直接调用targetSource的getTargetClass方法
        //其实也是相当于调用target.getClass()
        return this.targetSource.getTargetClass();
    }

OK,下面我们进入到AopUtils#findAdvisorsThatCanApply中看一下这个方法的内容

    public static List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvisors, Class<?> clazz) {
        //如果传入的Advisor集合为空的话,直接返回这个空集合
        //这里没有判断candidateAdvisors不为null的情况 因为在获取Advisor的地方是先创建一个空的集合,再进行添加Advisor的动作
        //不过还是加一下不为null的判断更好一点
        if (candidateAdvisors.isEmpty()) {
            return candidateAdvisors;
        }
        //创建一个合适的Advisor的集合 eligible
        List<Advisor> eligibleAdvisors = new LinkedList<Advisor>();
        //循环所有的Advisor
        for (Advisor candidate : candidateAdvisors) {
            //如果Advisor是IntroductionAdvisor  引介增强 可以为目标类 通过AOP的方式添加一些接口实现
            //引介增强是一种比较我们接触的比较少的增强 我们可以在以后的文章单独做个分析
            if (candidate instanceof IntroductionAdvisor && canApply(candidate, clazz)) {
                eligibleAdvisors.add(candidate);
            }
        }
        //是否有引介增强
        boolean hasIntroductions = !eligibleAdvisors.isEmpty();
        for (Advisor candidate : candidateAdvisors) {
            //如果是IntroductionAdvisor类型的话 则直接跳过
            if (candidate instanceof IntroductionAdvisor) {
                // already processed
                continue;
            }
            //判断此Advisor是否适用于target
            if (canApply(candidate, clazz, hasIntroductions)) {
                eligibleAdvisors.add(candidate);
            }
        }
        return eligibleAdvisors;
    }

canApply方法的内容

    public static boolean canApply(Advisor advisor, Class<?> targetClass, boolean hasIntroductions) {
        //如果是IntroductionAdvisor的话,则调用IntroductionAdvisor类型的实例进行类的过滤
        //这里是直接调用的ClassFilter的matches方法
        if (advisor instanceof IntroductionAdvisor) {
            return ((IntroductionAdvisor) advisor).getClassFilter().matches(targetClass);
        }
        //通常我们的Advisor都是PointcutAdvisor类型
        else if (advisor instanceof PointcutAdvisor) {
            PointcutAdvisor pca = (PointcutAdvisor) advisor;
            //这里从Advisor中获取Pointcut的实现类 这里是AspectJExpressionPointcut
            return canApply(pca.getPointcut(), targetClass, hasIntroductions);
        }
        else {
            // It doesn't have a pointcut so we assume it applies.
            return true;
        }
    }

重载canApply方法的内容。

    public static boolean canApply(Pointcut pc, Class<?> targetClass, boolean hasIntroductions) {
        Assert.notNull(pc, "Pointcut must not be null");
        //进行切点表达式的匹配最重要的就是 ClassFilter 和 MethodMatcher这两个方法的实现。
        //MethodMatcher中有两个matches方法。一个参数是只有Method对象和targetclass,另一个参数有
        //Method对象和targetClass对象还有一个Method的方法参数 他们两个的区别是:
        //两个参数的matches是用于静态的方法匹配 三个参数的matches是在运行期动态的进行方法匹配的
        //先进行ClassFilter的matches方法校验
        //首先这个类要在所匹配的规则下
        if (!pc.getClassFilter().matches(targetClass)) {
            return false;
        }
        //再进行 MethodMatcher 方法级别的校验
        MethodMatcher methodMatcher = pc.getMethodMatcher();
        if (methodMatcher == MethodMatcher.TRUE) {
            // No need to iterate the methods if we're matching any method anyway...
            return true;
        }

        IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null;
        if (methodMatcher instanceof IntroductionAwareMethodMatcher) {
            introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher) methodMatcher;
        }

        Set<Class<?>> classes = new LinkedHashSet<Class<?>>(ClassUtils.getAllInterfacesForClassAsSet(targetClass));
        classes.add(targetClass);
        for (Class<?> clazz : classes) {
            Method[] methods = ReflectionUtils.getAllDeclaredMethods(clazz);
            //只要有一个方法能匹配到就返回true
            //这里就会有一个问题:因为在一个目标中可能会有多个方法存在,有的方法是满足这个切点的匹配规则的
            //但是也可能有一些方法是不匹配切点规则的,这里检测的是只有一个Method满足切点规则就返回true了
            //所以在运行时进行方法拦截的时候还会有一次运行时的方法切点规则匹配
            for (Method method : methods) {
                if ((introductionAwareMethodMatcher != null &&
                        introductionAwareMethodMatcher.matches(method, targetClass, hasIntroductions)) ||
                        methodMatcher.matches(method, targetClass)) {
                    return true;
                }
            }
        }

        return false;
    }

从上面的代码来看,这次我们要分析的重点就在AspectJExpressionPointcut这个类中了。在AspectJExpressPointcut中预先初始化了这些内容:你能看出来这是什么内容吗?

    static {
        //execution
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.EXECUTION);
        //args
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.ARGS);
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.REFERENCE);
        //this
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.THIS);
        //target
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.TARGET);
        //within
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.WITHIN);
        //@annotation
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_ANNOTATION);
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_WITHIN);
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_ARGS);
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_TARGET);
    }

我们来看一下这段代码,这是要从AspectJExpressPointcut中获取ClassFilter

        if (!pc.getClassFilter().matches(targetClass)) {
            return false;
        }
    public ClassFilter getClassFilter() {
        checkReadyToMatch();
        return this;
    }
    private void checkReadyToMatch() {
        //如果没有expression的值的话 直接抛出异常
        if (getExpression() == null) {
            throw new IllegalStateException("Must set property 'expression' before attempting to match");
        }
        if (this.pointcutExpression == null) {
            //选择类加载器
            this.pointcutClassLoader = determinePointcutClassLoader();
            //构建PointcutExpression的实例
            this.pointcutExpression = buildPointcutExpression(this.pointcutClassLoader);
        }
    }
    //下面的这一段逻辑完全就是aspectj这个jar中的内容了 很复杂不多说了。。。。
    private PointcutExpression buildPointcutExpression(ClassLoader classLoader) {
        //初始化一个PointcutParser的实例 PointcutParser   aspectj中提供的类
        PointcutParser parser = initializePointcutParser(classLoader);
        PointcutParameter[] pointcutParameters = new PointcutParameter[this.pointcutParameterNames.length];
        for (int i = 0; i < pointcutParameters.length; i++) {
            pointcutParameters[i] = parser.createPointcutParameter(
                    this.pointcutParameterNames[i], this.pointcutParameterTypes[i]);
        }
        //解析切点表达式 我们的切点表示有可能会这样写:在切面中定义一个专门的切面表达式方法
        //在不同的通知类型中引入这个切点表达式的方法名 
        return parser.parsePointcutExpression(replaceBooleanOperators(getExpression()),
                this.pointcutDeclarationScope, pointcutParameters);
    }
    //这个方法 将表达式中的 and 替换为 && or 替换为 ||   not 替换为 !
    private String replaceBooleanOperators(String pcExpr) {
        String result = StringUtils.replace(pcExpr, " and ", " && ");
        result = StringUtils.replace(result, " or ", " || ");
        result = StringUtils.replace(result, " not ", " ! ");
        return result;
    }

由于我们在项目开发中使用SpringAOP基本上都是用的AspectJ的注解的形式。AspectJ对于切点的匹配规则解析是一个比较复杂的过程,所以我们在使用的AspectJ中的切点表达式的时候要按照它的一个规则来进行书写。

相关文章
|
1月前
|
监控 Java 开发者
Spring AOP动态代理
Spring AOP动态代理
40 1
|
22天前
|
设计模式 Java Maven
Spring Aop 底层责任链思路实现-springaopdi-ceng-ze-ren-lian-si-lu-shi-xian
Spring Aop 底层责任链思路实现-springaopdi-ceng-ze-ren-lian-si-lu-shi-xian
31 1
|
20天前
|
存储 XML 缓存
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache功能的开发实战指南(一)
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache功能的开发实战指南
42 0
|
15天前
|
XML Java Maven
Spring之Aop的注解使用
Spring之Aop的注解使用
|
21天前
|
Java Spring
Spring 如何实现 AOP
Spring 如何实现 AOP
17 0
|
30天前
|
Java 编译器 程序员
Spring AOP 和 AspectJ 的比较
Spring AOP 和 AspectJ 的比较
34 0
|
1月前
|
Java Spring
【spring(三)】AOP总结
【spring(三)】AOP总结
|
Java 应用服务中间件 数据库连接
Spring全家桶之Spring篇深度分析(一)
Spring 框架不局限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何 Java 应用都可以从 Spring 中受益。Spring 框架还是一个超级粘合平台,除了自己提供功能外,还提供粘合其他技术和框架的能力。
Spring全家桶之Spring篇深度分析(一)
|
21天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
38 0
|
29天前
|
缓存 安全 Java
Spring Boot 面试题及答案整理,最新面试题
Spring Boot 面试题及答案整理,最新面试题
106 0