Spring 高级 切点匹配

一、execution 和 annotation 两种方式设置切点匹配

复制代码
package com.mangoubiubiu.show.a16;

import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.transaction.annotation.Transactional;

public class A16 {
    public static void main(String[] args) throws NoSuchMethodException {
        //根据切点表达式判断目标方法是否需要增强
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();

        //设置切点
        pointcut.setExpression("execution(* bar())");

        System.out.println(pointcut.matches(T1.class.getMethod("foo"), T1.class));
        System.out.println(pointcut.matches(T1.class.getMethod("bar"), T1.class));


        AspectJExpressionPointcut pointcut2 = new AspectJExpressionPointcut();

        //利用注解标注
        pointcut2.setExpression("@annotation(org.springframework.transaction.annotation.Transactional)");

        System.out.println(pointcut2.matches(T1.class.getMethod("foo"), T1.class));
        System.out.println(pointcut2.matches(T1.class.getMethod("bar"), T1.class));


    }



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

    @Transactional
    static class T2 {
        public void foo() {
        }
    }

    @Transactional
    interface I3 {
        void foo();
    }
    static class T3 implements I3 {
        public void foo() {
        }
    }
}
复制代码

 

二、Transactional

Transactional 注解可以标注在类上,方法上,接口上 annotation只能匹配方法上加没有加注解。

Transactional 的作用域可以作用在 方法上 类上 接口上 如果一个类继承了 含有Transactional 的接口 那么它也生效。

复制代码
package com.mangoubiubiu.show.a16;

import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;

public class A16 {
    public static void main(String[] args) throws NoSuchMethodException {
        //根据切点表达式判断目标方法是否需要增强
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();

        //设置切点
        pointcut.setExpression("execution(* bar())");

//        System.out.println(pointcut.matches(T1.class.getMethod("foo"), T1.class));
//        System.out.println(pointcut.matches(T1.class.getMethod("bar"), T1.class));


        AspectJExpressionPointcut pointcut2 = new AspectJExpressionPointcut();

        //利用注解标注
        pointcut2.setExpression("@annotation(org.springframework.transaction.annotation.Transactional)");

//        System.out.println(pointcut2.matches(T1.class.getMethod("foo"), T1.class));
//        System.out.println(pointcut2.matches(T1.class.getMethod("bar"), T1.class));


        StaticMethodMatcherPointcut pt = new StaticMethodMatcherPointcut() {
            @Override
            public boolean matches(Method method, Class<?> targetClass) {

                //检查方法上是否加了 Transactional 注解
                MergedAnnotations mergedAnnotations = MergedAnnotations.from(method);
                if(mergedAnnotations.isPresent(Transactional.class)){
                    return true;
                }
                //检查类上是否加了 Transactional 注解 查找范围是本类
               // mergedAnnotations = MergedAnnotations.from(targetClass);
                //检查类上是否加了 Transactional 注解 查找范围是继承树 不仅搜索本类还会搜索父类,接口也会查一遍
                mergedAnnotations = MergedAnnotations.from(targetClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY);

                if(mergedAnnotations.isPresent(Transactional.class)){
                    return true;
                }


                return false;
            }
        };

        //查看方法上是否标注了Transactional
        System.out.println(pt.matches(T1.class.getMethod("bar"), T1.class));
        System.out.println(pt.matches(T1.class.getMethod("foo"), T1.class));


        //查看类上是否标注了Transactional
        System.out.println(pt.matches(T2.class.getMethod("foo"), T2.class));



        //查看类继承的接口上是否标注了Transactional
        System.out.println(pt.matches(T3.class.getMethod("foo"), T3.class));
    }



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

    @Transactional
    static class T2 {
        public void foo() {
        }
    }

    @Transactional
    interface I3 {
        void foo();
    }
    static class T3 implements I3 {
        public void foo() {
        }
    }
}
复制代码

 

 

 

本文作者:KwFruit

本文链接:https://www.cnblogs.com/mangoubiubiu/p/16750516.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   KwFruit  阅读(101)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起