1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      SpringBootAOP怎么用

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

      創(chuàng)新互聯(lián)建站網(wǎng)站建設(shè)由有經(jīng)驗的網(wǎng)站設(shè)計師、開發(fā)人員和項目經(jīng)理組成的專業(yè)建站團隊,負(fù)責(zé)網(wǎng)站視覺設(shè)計、用戶體驗優(yōu)化、交互設(shè)計和前端開發(fā)等方面的工作,以確保網(wǎng)站外觀精美、網(wǎng)站設(shè)計、成都做網(wǎng)站易于使用并且具有良好的響應(yīng)性。

      1. 啟用AOP

      a. 在類上添加@Aspect注解

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

      2. 通過PointCut對象創(chuàng)建切入點

      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. 方法內(nèi)可以什么也不寫, 寫了也調(diào)不到
      iv. 也可以創(chuàng)建多個PointCut,例如再創(chuàng)建一個

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

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

      b. 創(chuàng)建After方法,Before方法

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

      i. After方法是指, 在配置了的切入點被執(zhí)行后, 執(zhí)行該方法. 
      ii. value中的pointcut() 是我們前面在創(chuàng)建@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是指在被切入的方法執(zhí)行后, 獲取其返回值, 再執(zhí)行該方法. 注意關(guān)鍵, 這個可以進行操作返回值. 
      ii. returning = "str",是指, 假設(shè)切入方法的返回的值變量名為str
      doAfterReturning(String str)方法的參數(shù)變量名必須和和returning保持一致, 這里也叫作str. 然后才能在方法體中使用.
      iii. pointcut = "pointcut()"同樣是指前面聲明的pointcut方法名

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

      a. 監(jiān)聽方法參數(shù)

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

      4. 執(zhí)行順序:

      a.Around的方法優(yōu)先于Before/After執(zhí)行,After優(yōu)先于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;
            }
          }

      執(zhí)行結(jié)果:

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

      5.小結(jié):

        @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 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 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 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怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


      當(dāng)前名稱:SpringBootAOP怎么用
      鏈接地址:http://www.ef60e0e.cn/article/pjcjcc.html 99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
          1. <ul id="0c1fb"></ul>

            <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
            <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

            高雄市| 蓬溪县| 彭水| 青岛市| 营山县| 琼海市| 德江县| 富裕县| 渭源县| 桦川县| 桐城市| 台湾省| 延长县| 吴忠市| 翁牛特旗| 长寿区| 南岸区| 虹口区| 左权县| 崇州市| 灵武市| 丹阳市| 蛟河市| 余姚市| 舞阳县| 睢宁县| 绩溪县| 宜州市| 木里| 阿克| 通山县| 天长市| 元氏县| 台山市| 宝山区| 乌兰县| 潮州市| 西和县| 洞口县| 阜城县| 紫阳县|