1. 程式人生 > >spring --自定義註解(配合@Aspect)

spring --自定義註解(配合@Aspect)

IT ng- runtime 自定義 alt type cee pan cut

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)