AOP相關一

package com.example.demo.retry;

import com.example.demo.aop.RetryAnno;
import com.example.demo.aop.RetryUtil1;
import com.example.demo.aop.Task;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @describtion aop異常通知
 * @creator wangli66
 * @create-time 14:19 2019/11/21
 **/
@Aspect
@Component
public class BizAspect {

    /*
     * @Description: 在註解中直接定義切點的方式
     * @param: [e]
     * @return: void
     * @Date: 2019/11/26
     */
    @AfterThrowing(throwing = "e",pointcut = "execution(* com.example.demo.retry.Bizwork.*(..))")
    public void testThrowing(Exception e) {
        System.out.println("aspect.........");
    }

    /*
     * @Description: 單獨定義切點
     * @param: []
     * @return: void
     * @Date: 2019/11/26
     */
    @Pointcut("execution(* com.example.demo.retry.Bizwork.*(..))")
    public void workCut() {
    }

    /*
     * @Description: 引入切點
     * @param: [joinPoint]
     * @return: void
     * @Date: 2019/11/26
     */
    @AfterThrowing("workCut()")
    public void testPointCut(JoinPoint joinPoint) {
        System.out.println("testPointCut.......");
        Task task = new Task();
        // 設置參數
        task.setTargetClass(joinPoint.getSignature().getDeclaringTypeName());
        task.setTargetMethod(joinPoint.getSignature().getName());
        task.setArgs(joinPoint.getArgs());
        task.setTimes(3);
        task.setWaitTime(3000L);

        // 調用重試工具類
        RetryUtil1.retryByTask(task);
    }

    /*
     * @Description: 使用自定義註解方式
     * @param: []
     * @return: void
     * @Date: 2019/11/26
     */
    @Pointcut(value = "@annotation(com.example.demo.aop.RetryAnno)")
    public void testAnnoCut() {

    }

    /*
     * @Description: 註解方式
     * @param: [joinPoint]
     * @return: void
     * @Date: 2019/11/21
     */
    @AfterThrowing(value = "@annotation(com.example.demo.aop.RetryAnno) && @annotation(retryAnno)")
    public void testAnnotion(JoinPoint joinPoint,RetryAnno retryAnno) {
        System.out.println("----註解方式------------------------"+retryAnno.maxAttempt());
        Task task = new Task();
        // 設置參數
        task.setTargetClass(joinPoint.getSignature().getDeclaringTypeName());
        task.setTargetMethod(joinPoint.getSignature().getName());
        task.setArgs(joinPoint.getArgs());
        task.setTimes(retryAnno.maxAttempt());
        task.setWaitTime(retryAnno.waitDesc());
        // 調用重試工具類
        RetryUtil1.retryByTask(task);

    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章