Spring Aspect 獲取請求參數


切片(Aspect)也就是Spring AOP

實現Aspect的主要步驟:

1、在哪里切入

  。在哪個方法起作用

  。什么時候起作用

2、起作用的時候執行什么處理邏輯

下面是代碼實現

/**
 * 切片Aspect @Around的使用
 * 1、添加 @Aspect 和@Component注解 
 * 2、方法是用@Around注解和傳入ProceedingJoinPoint對象
 * 雖然切片可以拿到是調用方法時候傳過來的參數和值
 * 但是....卻拿不了原始的請求和響應對象了
 *
 *
 */
@Aspect
@Component
public class DemoAspect {
    /**
     * 切入點(主要是注解)
     *
     * 1. 哪些方法上起作用
     *
     * 2.在什么時候起作用
     *
     * 相關注解有4個
     * 1.@Before 調用方法前
     * 2.@After  調用方法后
     * 3.@Afterth 方法拋出異常的時候
     * 4.@Around 包括了before、after、afterth 所以我們一般使用around
     *
     *
     * @Around 有專門的表達式 見官方文檔 https://docs.spring.io/spring/docs/5.2.0.BUILD-SNAPSHOT/spring-framework-reference/core.html#aop-ataspectj
     * 有關切入點的語法和使用
     *
     *  當前例子是在com.xiluo.web.controller.DemoController里面的任何方法
     *  注意的是要傳入ProceedingJoinPoint對象,這個對象記錄了你當前攔截到的方法的信息
     */
    @Around("execution(* com.xiluo.web.controller.DemoController.*(..))")
    public Object handelControllerMethod(ProceedingJoinPoint point) throws Throwable {
        System.out.println("aspect start ");
      
        //獲取參數
        Object[] args = point.getArgs();

        for (Object arg : args) {
            System.out.println("arg ==>" + arg);
        }

        //去調用被攔截的方法
        Object proceed = point.proceed();

        return proceed;
    }

}

 @Around注解

詳細使用見官方文檔:https://docs.spring.io/spring/docs/5.2.0.BUILD-SNAPSHOT/spring-framework-reference/core.html#aop-ataspectj  

 

 


免責聲明!

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



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