spring --自定义注解(配合@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();
    
}

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

猜你喜欢

转载自www.cnblogs.com/wenq001/p/9116120.html