首页 > 编程语言 > 详细

spring --自定义注解(配合@Aspect)

时间:2018-05-31 13:12:00      阅读:230      评论:0      收藏:0      [点我收藏+]

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();
    
}

2:定义切面处理类

@Aspect
@Component
@Slf4j
public class DistributedTransactionAspect implements Ordered{
    
    @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;
    }

    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE;
    }

}

定义切面处理类关键点:

  1.   类用@Aspect

spring --自定义注解(配合@Aspect)

原文:https://www.cnblogs.com/wenq001/p/9116120.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!