免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

SpringBootAOP怎么用

小編給大家分享一下SpringBoot AOP怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創新互聯建站網站建設由有經驗的網站設計師、開發人員和項目經理組成的專業建站團隊,負責網站視覺設計、用戶體驗優化、交互設計和前端開發等方面的工作,以確保網站外觀精美、網站設計、成都做網站易于使用并且具有良好的響應性。

1. 啟用AOP

a. 在類上添加@Aspect注解

b. 注入該類, 可以使用@Component進行注入到Spring容器中

2. 通過PointCut對象創建切入點

a. 在某個方法使用類似下面的方法進行注入

@Pointcut("execution(* com.sguess.service.IAOPService.*(..))")
  private void pointcut() {
  }

i. 其中,execution表達式為
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)  
ii. 注意, pointcut()方法名是后面切入的時候需要使用的
iii. 方法內可以什么也不寫, 寫了也調不到
iv. 也可以創建多個PointCut,例如再創建一個

@Pointcut("execution(* com.sguess.service.IAOPService.fun1(..))")
    private void pointcut2() {
    }

這個的方法名就位pointcut2, 方法名不一樣.  

b. 創建After方法,Before方法

@After(value = "pointcut()")
  public void doAfter() {
    System.out.println("Do AOP After function 01");
  }

i. After方法是指, 在配置了的切入點被執行后, 執行該方法. 
ii. value中的pointcut() 是我們前面在創建@Pointcut中的方法名. 也就是說,是通過方法名和切入點進行匹配的. 
iii. 這個的方法名可以隨便起. 
iv. Before方法同理

c. 帶Return的After方法,

@AfterReturning(returning = "str", pointcut = "pointcut()")
  public void doAfterReturning(String str) throws Exception {
    System.out.println("Return value is: " + str);
  }

i. AfterReturn是指在被切入的方法執行后, 獲取其返回值, 再執行該方法. 注意關鍵, 這個可以進行操作返回值. 
ii. returning = "str",是指, 假設切入方法的返回的值變量名為str
doAfterReturning(String str)方法的參數變量名必須和和returning保持一致, 這里也叫作str. 然后才能在方法體中使用.
iii. pointcut = "pointcut()"同樣是指前面聲明的pointcut方法名

3. 通過注解, 使用切入點

a. 監聽方法參數

@Before("execution(public int com.sguess.service.*(int, int))")
  public void beforMethod(JoinPoint point) {
    String methodName = point.getSignature().getName();
    List<Object> args = Arrays.asList(point.getArgs());
    System.out.println("Before FunctionName:" + methodName + ",ParameterName:" + args);
  }
  @After("execution(public int com.sguess.service.*(int, int))")
  public void afterMethod(JoinPoint point) {
    String methodName = point.getSignature().getName();
    List<Object> args = Arrays.asList(point.getArgs());
    System.out.println("After FunctionName:" + methodName + ",ParameterName:" + args);
  }

4. 執行順序:

a.Around的方法優先于Before/After執行,After優先于AfterReturn. 

i. 代碼

@Before("execution(public int com.sguess.service.*.*(int, int))")
      public void beforMethod(JoinPoint point) {
        System.out.println("Before function");
      }
      @After("execution(public int com.sguess.service.*.*(int, int))")
      public void afterMethod(JoinPoint point) {
        System.out.println("After function");
      }
      @AfterReturning("execution(public int com.sguess.service.*.*(int, int))")
      public void afterReturnMethod(JoinPoint point) {
        System.out.println("AfterReturn function");
      }
      @AfterThrowing(value = "execution(public int com.sguess.service.*.*(int, int))", throwing = "e")
      public void afterReturningThrowing(JoinPoint point, Exception e) {
        System.out.println("AfterReturnThrowing function");
      }
      @Around("execution(public int com.sguess.service.*.*(int, int))")
      public Object aroundMethod(ProceedingJoinPoint pdj) {
        System.out.println("Start AroundFunction");
        Object result = null;
        try {
          System.out.println("Around process start");
          result = pdj.proceed();
          System.out.println("Around process end");
        } catch (Throwable e) {
          System.out.println("Around process exception");
        }
        System.out.println("After Around process");
        return result;
      }
    }

執行結果:

Start AroundFunction
Around process start
Before function
Around process end
After Around process
After function
AfterReturn function

5.小結:

  @AfterReturning(returning = "str", pointcut = "pointcut()")
  public void doAfterReturning(String str) throws Exception {
    System.out.println("Return value is: " + str);
  }
  @Before("execution(public int com.sguess.service.*.*(int, int))")
  public void beforMethod(JoinPoint point) {
    String methodName = point.getSignature().getName();
    List<Object> args = Arrays.asList(point.getArgs());
    System.out.println("Before FunctionName:" + methodName + ",ParameterName:" + args);
  }
  @After("execution(public int com.sguess.service.*.*(int, int))")
  public void afterMethod(JoinPoint point) {
    String methodName = point.getSignature().getName();
    List<Object> args = Arrays.asList(point.getArgs());
    System.out.println("After FunctionName:" + methodName + ",ParameterName:" + args);
  }
  @AfterThrowing(value = "execution(public int com.sguess.service.*.*(int, int))", throwing = "e")
  public void afterReturningThrowing(JoinPoint point, Exception e) {
    String methodName = point.getSignature().getName();
    List<Object> args = Arrays.asList(point.getArgs());
    System.out.println("AfterReturningThrowing FunctionName:" + methodName + ",ParameterName:" + args + ",Exception:" + e);
  }
  @Around("execution(public int com.sguess.service.*.*(int, int))")
  public Object aroundMethod(ProceedingJoinPoint pdj) {
      System.out.println("Start AroundFunction");
      Object result = null;
      try {
          System.out.println("Around process start");
          result = pdj.proceed();
          System.out.println("Around process end");
      } catch (Throwable e) {
          System.out.println("Around process exception");
      }
      System.out.println("After Around process");
      return result;
  }

以上是“SpringBoot AOP怎么用”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創新互聯行業資訊頻道!

文章名稱:SpringBootAOP怎么用
轉載注明:http://m.newbst.com/article22/pjcjcc.html

成都網站建設公司_創新互聯,為您提供響應式網站網站制作外貿建站自適應網站定制開發軟件開發

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

外貿網站建設