Java-SpringBoot實現AOP (@Around)


AOP基本總結


  • 連接點(JoinPoint):

    • 連接點是程序運行的某個階段點,如方法調用、異常拋出等
  • 切入點(Pointcut):

    • 切入點是JoinPoint的集合
    • 是程序中需要注入Advice的位置的集合,即Advice在什么條件下才能被觸發
  • 增強(Advisor):

    • 增強是切入點Pointcut和Advice的綜合體,即在連接點JoinPoint上執行的行為
    • 通過JDK/CGLIB代理模式實現AOP
  • 切面(Aspect):

    • @Aspect通常是一個類的注解,通常與@Component搭配使用
  • AOP代理(AOP Proxy):

    • AOP使用動態代理模式創建對象,從而實現在連接點JoinPoint處插入增強
    • 其中JDK只能代理接口,CGLIB基於子類但不能代理final類

 

常用方法

方法   描述
Object[] getArgs 返回目標方法的參數數組
Signature getSignature 返回目標方法所在類信息
Object getTarget 返回被織入增強處理的目標對象
Object getThis

返回AOP代理對象

Object proceed(Object[] args)

利用反射執行目標方法並返回結果

 

增強類型

  • @Before:前置增強,在某個JoinPoint執行前的增強

  • @After:final增強,不管拋異常還是正常退出都執行的增強

  • @AfterReturning:后置增強,方法正常退出時執行

  • @AfterThrowing:異常拋出增強,拋出異常后執行

  • @Around:環繞增強,包圍一個連接點的增強,最強大的一個方式,且常用

 

示例說明

學了一下AOP的使用,寫了個@Around的demo,將幾個查詢操作存入數據庫作為Log並且定時清理過期數據

本人的Demo用的是Dubbo框架,而AOP的示例寫在了Provider中,大概結構如下:

  • monitor:
    • annotation:注解類
    • aop:切面的定義及實現
    • impl:UserAopTask接口的實現類

 

1)UserLog實體類

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class UserLog implements Serializable {
    private Integer id;
    private String methodName;
    private String methodArgs;
    private String classFullName;
    private String className;
    private Date invokeTime;
    private Double costTime;
}

 

2)LogTaskMapper對應mapper接口

public interface LogTaskMapper {
    /**
     * TEST AOP INSERT INFO INTO TABLE
     * @param userLog
     */
    void insertUserLog(UserLog userLog);

    /**
     * DELETE LOGS IN TABLE LAST x MINUTES
     * @param minutes
     */
    void deleteUserLog(int minutes);
}

 

3)UserAopTask接口

public interface UserAopTask {
    void insertUserLog(UserLog log);
}

 

4)UserAopTaskImpl實現類

@Component
public class UserAopTaskImpl implements UserAopTask {

    private static final Logger logger = LoggerFactory.getLogger(UserAopTask.class);

    @Autowired
    private LogTaskMapper logTaskMapper;

    private ExecutorService logHandler = Executors.newFixedThreadPool(1);//采用線程池復用一個線程執行

    private static final int MINUTES_LOG_RETAIN = 30;//數據庫中數據保留時間


    @Override
    public void insertUserLog(UserLog log) {
        logHandler.submit(new logSubmitTask(log));
    }

    //內部類
    class logSubmitTask implements Runnable{

        private UserLog userLog;

        public logSubmitTask(UserLog userLog){
            this.userLog = userLog;
        }

        @Override
        public void run() {
            logTaskMapper.insertUserLog(userLog);
        }
    }

    //定時清理任務
    @Scheduled(cron = "0 30 * * * *")
    public void scheduledDeleteLog(){
        logger.info("開始清除[{}]分鍾之前的圖表查詢日志...", MINUTES_LOG_RETAIN);
        logTaskMapper.deleteUserLog(-1 * MINUTES_LOG_RETAIN);
    }

}

 

5)TestUserAop切面類

@Aspect//切面
@Component//Spring容器管理
public class TestUserAop {
    private static final Logger logger = LoggerFactory.getLogger(TestUserAop.class);

    @Autowired
    private UserAopTask userAopTask;
   //使用環繞增強,第一參數必須是ProceedingJoinPoint
    @Around(value = "@annotation(annotation)")//和注解類參數名保持一致
    public Object aroundUserInfo(ProceedingJoinPoint pjp, TestUserAnnotation annotation) throws Throwable{
        UserLog userLog = new UserLog();

        System.out.println("=====================ANNOTATION BEGIN=====================");

        Date date = new Date();
        Long methodStart = date.getTime();//timestamp
        System.out.println("ANNOTATION 開始耗時統計: "+ date);

        userLog.setInvokeTime(date);

        Object[] argsObj = pjp.getArgs();
        Object res = pjp.proceed(argsObj);//利用反射調用目標方法

        Long methodCost = System.currentTimeMillis() - methodStart;
        double cost = methodCost/1000d;//timestamp 轉換為 seconds

        System.out.println("ANNOTATION 調用方法總耗時: "+ String.format("%.3f",cost) +" s");//保留3位小數
        System.out.println("ANNOTATION 調用方法: "+annotation.methodName());//目標方法
        System.out.println("ANNOTATION 調用方法參數: "+ new Integer((Integer) argsObj[0]));//我的參數就1個或者無參
        System.out.println("ANNOTATION 調用類: "+pjp.getSignature().getDeclaringTypeName());//全類名
        System.out.println("ANNOTATION 調用類名: "+pjp.getSignature().getDeclaringType().getSimpleName());//類名
        System.out.println("ANNOTATION 調用結果: "+ JSON.toJSON(res));
        System.out.println("=====================ANNOTATION FINISHED=====================");

        userLog.setCostTime(Double.parseDouble(String.format("%.3f",cost)));
        userLog.setClassFullName(pjp.getSignature().getDeclaringTypeName());
        userLog.setClassName(pjp.getSignature().getDeclaringType().getSimpleName());
        userLog.setMethodName(annotation.methodName());
        userLog.setMethodArgs(Integer.toString(new Integer((Integer) argsObj[0])));

        userAopTask.insertUserLog(userLog);

        return res;

    }
}

 

6)TestUserAnnotation注解類

我在service層寫的AOP demo,對目標方法使用注解,注解名為注解類名即可,如@TestUserAnnotation

@Retention(RetentionPolicy.RUNTIME)//運行時有效
@Target(ElementType.METHOD)//作用於方法
@Documented
public @interface TestUserAnnotation {
    String methodName() default "";//方法名,默認為空字符串
}

 

7)LogTaskMapper.xml

最后貼個代碼,為上面提到的定時任務,用到的是date_add()方法,其中的 "&lt;" 意為 "<"

<delete id="deleteUserLog" parameterType="java.lang.Integer">
        delete from invoke_log
        where invoke_time &lt; date_add(current_timestamp,interval #{minutes} minute)
</delete>

 

結果展示

演示一下AOP的效果,將@TestUserAnnotation注解在方法getUserInfo(),即獲取用戶信息

Demo中利用AOP的@Around環繞增強,實現了統計方法調用運行消耗時間,以及統計調用方法名、類名等信息:

 

調用方法getUserInfo后的統計結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM