<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] # 1. 前置增強 (1)前置增強配置。 ```xml <aop:config> <aop:pointcut id="add" expression="execution(public void add(int, int))" /> <aop:aspect ref="aspectLogger"> <aop:before method="printLoggerBefore" pointcut-ref="add" /> </aop:aspect> </aop:config> ``` (2)增強函數。 ```java public void printLoggerBefore(JoinPoint jPoint) { logger.info("\n------增強函數的輸出------" + "\n增強方式:前置增強" + "\n目標對象:" + jPoint.getTarget() + "\n目標函數:" + jPoint.getSignature().getName() + "\n目標函數的參數:" + Arrays.toString(jPoint.getArgs())); } ``` (3)測試結果,前置增強函數的輸出要在目標函數的前面。 ``` ------增強函數的輸出------ 增強方式:前置增強 目標對象:com.learn.spring.aop.service.impl.StudentServiceImpl@515aebb0 目標函數:add 目標函數的參數:[10, 20] ------目標函數的輸出------ 30 ``` * 前置增強先于目標函數運行。 * 無論目標函數是否發生異常,前置增強都正常執行,相當于放在`finally`語句塊中執行。 * 不能獲取目標函數的返回值。 # 2. 最終增強 (1)最終增強配置。 ```xml <aop:config> <aop:pointcut id="add" expression="execution(public void add(int, int))" /> <aop:aspect ref="aspectLogger"> <aop:after method="printLoggerAfter" pointcut-ref="add" /> </aop:aspect> </aop:config> ``` (2)增強函數。 ```java public void printLoggerAfter(JoinPoint jPoint) { logger.info("\n------增強函數的輸出------" + "\n增強方式:最終增強" + "\n目標對象:" + jPoint.getTarget() + "\n目標函數:" + jPoint.getSignature().getName() + "\n目標函數的參數:" + Arrays.toString(jPoint.getArgs())); } ``` (3)測試結果,最終增強函數的輸出要在目標函數的后面。 ``` ------目標函數的輸出------ 30 ------增強函數的輸出------ 增強方式:最終增強 目標對象:com.learn.spring.aop.service.impl.StudentServiceImpl@7e07db1f 目標函數:add 目標函數的參數:[10, 20] ``` * 目標函數先于最終增強執行。 * 無論目標函數是否發生異常,最終增強都正常執行。 * 無法獲取目標函數的返回值。 # 3. 異常增強 (1)異常增強配置。 ```xml <aop:config> <aop:pointcut id="add" expression="execution(public void add(int, int))" /> <aop:aspect ref="aspectLogger"> <!-- throwing: 目標函數發生的異常,如果發生異常則由printLoggerThrowing函數捕捉 --> <aop:after-throwing method="printLoggerThrowing" pointcut-ref="add" throwing="e" /> </aop:aspect> </aop:config> ``` (2)增強函數。 ```java /** * @param e e的命名與 throwing="e" 要一致 */ public void printLoggerThrowing(JoinPoint jPoint, Exception e) { logger.info("\n------增強函數的輸出------" + "\n增強方式:異常增強" + "\n目標對象:" + jPoint.getTarget() + "\n目標函數:" + jPoint.getSignature().getName() + "\n目標函數的參數:" + Arrays.toString(jPoint.getArgs()) + "\n目標函數的異常:" + e); } ``` (3)測試結果,只有當目標函數發生異常時異常增強才執行。 ``` ########## 沒有發生異常的結果 ########### ------目標函數的輸出------ 30 ########## 發生異常的結果 ########### ------增強函數的輸出------ 增強方式:異常增強 目標對象:com.learn.spring.aop.service.impl.StudentServiceImpl@1b7cc17c 目標函數:add 目標函數的參數:[10, -20] 目標函數的異常:java.lang.Exception: (x+y)不能小于0. java.lang.Exception: (x+y)不能小于0. at com.learn.spring.aop.service.impl.StudentServiceImpl.add(StudentServiceImpl.java:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:47) ``` * 只有在目標函數發生異常時才執行。 * 目標函數先于異常增強執行。 * 不能獲取目標函數的返回值。 # 4. 后置增強 (1)后置增強配置。 ```xml <aop:config> <aop:pointcut id="sub" expression="execution(public int sub(int, int))" /> <aop:aspect ref="aspectLogger"> <!-- returning: 獲取目標函數的返回值,并發送到printLoggerReturning方法中--> <aop:after-returning method="printLoggerReturning" pointcut-ref="sub" returning="result" /> </aop:aspect> </aop:config> ``` (2)增強函數。 ```java /** * @param result result的命名與 returning="result" 要一致 */ public String printLoggerReturning(JoinPoint jPoint, Object result) { logger.info("\n------增強函數的輸出------" + "\n增強方式:后置增強" + "\n目標對象:" + jPoint.getTarget() + "\n目標函數:" + jPoint.getSignature().getName() + "\n目標函數的參數:" + Arrays.toString(jPoint.getArgs()) + "\n目標函數的返回值:" + result); return "add-" + result; } ``` (3)測試結果,可以獲取目標函數的返回值。 ``` ------目標函數的輸出------ 10 ------增強函數的輸出------ 增強方式:后置增強 目標對象:com.learn.spring.aop.service.impl.StudentServiceImpl@1283bb96 目標函數:sub 目標函數的參數:[20, 10] 目標函數的返回值:10 ``` * 只有目標函數不發生異常時才執行。 * 目標函數先于后置增強執行。 * 可以獲取目標函數的返回值,但不能修改目標函數的返回值,如果目標函數返回`void`,則result為`null`。 # 5. 環繞增強 (1)環繞增強配置。 ```xml <aop:config> <aop:pointcut id="sub" expression="execution(public int sub(int, int))" /> <aop:aspect ref="aspectLogger"> <aop:around method="printLoggerAround" pointcut-ref="sub" /> </aop:aspect> </aop:config> ``` (2)增強方法。 ```java /** * @param jPoint ProceedingJoinPoint接口是JoinPoint的子接口,它只能用在環繞增強中 * @return 環繞增強函數返回的值將作為目標函數的返回值 */ public int printLoggerAround(ProceedingJoinPoint jPoint) throws Throwable { Object[] args = jPoint.getArgs(); logger.info("\n------增強函數的輸出------" + "\n增強方式:環繞增強" + "\n目標對象:" + jPoint.getTarget() + "\n目標函數:" + jPoint.getSignature().getName() + "\n目標函數的參數:" + Arrays.toString(jPoint.getArgs())); jPoint.proceed(args); int result = ((int)args[0] - (int)args[1]) * 100; return result; } ``` (3)測試結果,成功修改了目標函數的返回值。 ``` ------增強函數的輸出------ 增強方式:環繞增強 目標對象:com.learn.spring.aop.service.impl.StudentServiceImpl@145f66e3 目標函數:sub 目標函數的參數:[20, 10] ------目標函數的輸出------ 10 返回結果:1000 ``` * 環繞增強在目標函數前后都執行。 * 環繞增強函數的返回值作為目標函數的返回值,即增強方法可以改變目標函數的返回值(注意:增強并不能獲取目標函數的返回值,但能修改)。 * 如果增強函數返回值為`void`,則目標函數的返回值為`null`。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看