zoukankan      html  css  js  c++  java
  • spring --解析自定义注解SpringAOP(配合@Aspect)

    1:首先,声明自定义注解

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface DtTransactional {
       
       /*
        * Whether need to rollback
        */
       public boolean includeLocalTransaction() default true;
       
       public boolean confirmMethodExist() default true;
       
       /*
        * Allow [confirmMethod] is null if [confirmMethodExist] is false
        */
        public String confirmMethod() default "";
    
        public String cancelMethod();
        
    }

    自定义注解定义的属性方法,如果没有 default “” ,则使用该注解时该属性为必填 ;

    2:定义切面处理类

    @Aspect
    @Component
    @Slf4j
    public class DistributedTransactionAspect{
        
        @Autowired
        private DistributedTransactionInterceptor distributedTransactionInterceptor;
    
        @Pointcut("@annotation(com.sysware.cloud.commons.dts.annotation.DtTransactional)")
        public void distributedTransactionService() {
    
        }
    
        @Around("distributedTransactionService()")
        public Object interceptDtTransactionalMethod(ProceedingJoinPoint pjp) throws Throwable {
            log.debug("interface-ITransactionRunning-start---->");
            Object obj =  distributedTransactionInterceptor.interceptDtTransactionalMethod(pjp);
            log.debug("interface-ITransactionRunning-end---->");
            return obj;
        }
    }

    定义切面处理类关键点:

      1:类上面用@Aspect 注解修饰 。

           2:定义切点,用@Pointcut("@annotation(com.sysware.cloud.commons.dts.annotation.DtTransactional)") 注解定义切点,

      表示扫描所有用@DtTransactional注解标识的方法 。

           3:用@Around @Before @After 注解标识,标识拦截方法的时机 ;@Before是在所拦截方法执行之前执行一段逻辑。

      @After 是在所拦截方法执行之后执行一段逻辑。@Around是可以同时在所拦截方法的前后执行一段逻辑。

           4:参数ProceedingJoinPoint pjp 通过pjp 可以获取注解信息,注解的参数,方法名,方法参数类型,方法参数等数据,然后对数据做一些统一处理。

  • 相关阅读:
    写一个精确定位异常的方法
    做一个牛XX的身份证号验证类(支持15位和18位)
    C#获取设备的IP和Mac类
    winfrom 倒计时控件
    一个实用价值很大的人脸关键点检测算法PFLD
    刷新WIDER Face纪录!TinaFace:人脸检测新网络,代码已开源!
    虚拟机安装教程
    python---文件路径的操作(有点意思)
    python_opencv -------->>>>>>>>>cv2.warpAffine()参数详解
    yolov5数据增强引发的思考——透视变换矩阵的创建
  • 原文地址:https://www.cnblogs.com/wenq001/p/9116120.html
Copyright © 2011-2022 走看看