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 可以获取注解信息,注解的参数,方法名,方法参数类型,方法参数等数据,然后对数据做一些统一处理。

  • 相关阅读:
    sublime text3 常用插件安装
    eclipse 快捷键
    数组去重及数组的prototype原型
    如何用angularjs给从后台传来数据添加链接
    【转载】Java Web的web.xml文件作用及基本配置
    【转】数据库几种范式
    Socket编程
    进程间的五种通信方式介绍
    进程与线程的区别
    【转载】程序员必知之浮点数运算原理详解
  • 原文地址:https://www.cnblogs.com/wenq001/p/9116120.html
Copyright © 2011-2022 走看看