<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ### spring aop嵌套調用問題 在使用 Spring AOP 的時候,我們從 IOC 容器中獲取的 Service Bean 對象其實都是代理對象,而不是那些 Service Bean 對象本身,也就是說獲取的并不是被代理對象或代理目標。當我在自己的 Service 類中使用 this 關鍵字嵌套調用同類中的其他方法時,由于 this 關鍵字引用的并不是該 Service Bean 對象的代理對象,而是其本身,故 Spring AOP 是不能攔截到這些被嵌套調用的方法的 當同一個類中對兩個方法增強后,一個方法調用另外一個方法時,產生了嵌套調用,其中一個切面就會失效 具體代碼: ~~~ public void hello() { System.out.println("Hello,IOC"); goodbye(); } public void goodbye() { System.out.println("Goodbye"); } @Pointcut("execution(public * io.github.dunwu.spring.core.aop.example.IOCServiceImpl.hello(..))") public void testAOP1(){ } @Around("testAOP1()") public Object around(ProceedingJoinPoint p){ System.out.println("around before testAOP1..."); Object o = null; try { o = p.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("around after testAOP1..."); return o; } @Pointcut("execution(public * io.github.dunwu.spring.core.aop.example.IOCServiceImpl.goodbye(..))") public void testAOP2(){ } @Around("testAOP2()") public void around(ProceedingJoinPoint p){ System.out.println("around before testAOP2..."); Object o = null; try { p.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("around after testAOP2..."); return o; } ~~~ 調用hello方法時,會發生什么呢? 只有hello方法被增強了,goodbye方法直接被調用,并沒有被增強。輸出如下: ~~~undefined around before testAOP1... Hello,IOC Goodbye around after testAOP1... ~~~ ** 為什么goodbye方法沒有被增強 ** 這是因為hello方法被增強之后,invoke hello方法時,調用goodbye其實是調用this.goodbye方法,也就是原始方法而并不是被增強的方法 ### 原理 ``` public interface ICustomerService { public void doSomething1(); public void doSomething2(); } public class CustomerServiceImpl implements ICustomerService { public void doSomething1() { System.out.println("Inside CustomerServiceImpl.doSomething1()"); doSomething2(); } public void doSomething2() { System.out.println("Inside CustomerServiceImpl.doSomething2()"); } } public class CustomerServiceProxy implements ICustomerService { private ICustomerService customerService; public void setCustomerService(ICustomerService customerService) { this.customerService = customerService; } public void doSomething1() { doBefore(); customerService.doSomething1(); doAfter(); } public void doSomething2() { doBefore(); customerService.doSomething2(); doAfter(); } private void doBefore() { // 例如,可以在此處開啟事務 System.out.println("do some important things before..."); } private void doAfter() { // 例如,可以在此處提交或回滾事務、釋放資源等等 System.out.println("do some important things after..."); } } public class TestProxy { public static void main(String[] args) { // 創建代理目標對象。對于Spring來說,這一工作 // 是由Spring DI容器完成的。 ICustomerService serviceProxyTarget = new CustomerServiceImpl(); // 創建代理對象。對于Spring來說,這一工作 // 也是由Spring DI容器完成的。 CustomerServiceProxy serviceProxy = new CustomerServiceProxy(); serviceProxy.setCustomerService(serviceProxyTarget); ICustomerService serviceBean = (ICustomerService) serviceProxy; // 調用業務邏輯操作 serviceBean.doSomething1(); } } ``` 現在以調試方式運行這個應用,你會發現在 doSomething1() 中調用 doSomething2() 方法的時候并未去執行CustomerServiceProxy 類的 doBefore()、doAfter() 方法。再來看看這句關鍵代碼:doSomething2(); 把它隱含的意思也表達出來吧:this.doSomething2(); 在CustomerServiceImpl類中this關鍵字表示的是當前這個CustomerServiceImpl類的實例。那程序當然就會去執行 CustomerServiceImpl 類中的 doSomething2() 方法了,而不會去執行 CustomerServiceProxy 類中的 doSomething2() 方法 ### 解決方案 1. 將自身注入到自身 ``` public class OneBean { @Resource private OneBean oneBean; public OneBean() { System.out.println("構造器OneBean加載..." + this); } public String say(String a) { System.out.println("say a=" + a); oneBean.say2(a); return a + a; } public String say2(String a) { System.out.println("say2 a=" + a); return a + a; } ``` 2. 使用AopContext.currentProxy()來操作 ``` public class OneBean { public OneBean() { System.out.println("構造器OneBean加載..." + this); } public String say(String a) { System.out.println("say a=" + a); ((OneBean)AopContext.currentProxy()).say2(a) ; return a + a; } public String say2(String a) { System.out.println("say2 a=" + a); return a + a; } } ```
                  <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>

                              哎呀哎呀视频在线观看