完整剖析SpringAOP的自調用


摘要

spring全家桶幫助java web開發者節省了很多開發量,提升了效率。但是因為屏蔽了很多細節,導致很多開發者只知其然,不知其所以然,本文就是分析下使用spring的一些注解,不能夠自調用的問題。因為本身這類文章很多,所以有些地方不會詳述,直接引用其他文章。

問題

  1. 使用了Spring中哪些注解不能進行自調用
  2. 為什么代理了就不能自調用
  3. Spring常用的 @Cache, @Async@Transaction 這三種原理上有什么區別嗎
  4. 如何解自調用的問題
  5. 使用不同的解法各自有什么坑

AOP的概述

首先需要澄清幾個需要區分的名詞 AOP Spring AOP AspectJ

AOP

Aspect-oriented programming,面向切面編程,一種解決問題的思想,將一些重復性的編碼問題通過切面來實現。
很多人了解切面是通過Spring來了解的,所以會有種誤解將SpringAOP和AOP划等號,其實不然。

Spring AOP

Spring AOP 算是一種簡單的AOP的落地實現方式,它主要提供在Spring容器內的一種AOP實現方式,脫離了Spring就不work了。Spring AOP並不是一套完整的AOP解決方案。

Spring的的眾多組件都是這樣,Spring-Session,Spring-jdbc,Spring-Cache等等,都能解決一部分通用的需求,但是會有很多限制,
想用深了,更靈活的實現功能,還是要使用其他的專業組件/框架。

SpringAOP默認使用代理模式實現的,也就是JDK Proxy/CGLib。關於代理以及JDK Proxy和CGLib不在贅述了。
在這里插入圖片描述

AspectJ

Spring AOP並不是一套完整的AOP解決方案,AspectJ是的。AspectJ在編譯器織入切面到目標類

解法

上面介紹了SpringAop的實現,下面着重介紹解法。

方法1 - 注入代理bean到自己

這個原理沒啥好解析的

    @Autowired
    @Lazy
    private AsyncMethod asyncMethod;
	  public void testAsync() {
        System.out.println(Thread.currentThread().getName());
		// 調用注入的bean
        asyncMethod.testAsnc3();
    }
    @Async
    public void testAsnc3() {
        System.out.println(Thread.currentThread().getName());
        System.out.println("async3");
    }

Note

會有循環依賴的問題,使用@Lazy解決

方法2 - AopContext.currentProxy() 獲取當前代理對象

使用

首先需要配置@EnableAspectJAutoProxy(exposeProxy = true),允許代碼中獲取proxy類

 public void testAsync() {
        System.out.println(Thread.currentThread().getName());
        System.out.println("async1");
      ((AsyncMethod)AopContext.currentProxy()).testAsnc2();
    }
	@Async
    public void testAsnc2() {
        System.out.println(Thread.currentThread().getName());
        System.out.println("async2");
    }

原理解析

這個實現可以看下AopContext類,

// 通過ThreadLocal來實現的
private static final ThreadLocal<Object> currentProxy = new NamedThreadLocal<Object>("Current AOP proxy");

然后就是Spring Aop自動設置代理,設置exposeProxy屬性的問題了。
有人寫過了,就不寫了

https://cloud.tencent.com/developer/article/1497700

Note

  1. 因為使用了SpringAOP,所以會有代理模式的限制
  2. AopContext.currentProxy()使用的是ThreadLocal的,所以不能跨線程了
  3. bean設置的限制,比如@Async代理創建方式不同其他|方式

方法3 - 直接使用AspectJ

既然自調用的問題是由於SpringAOP由代理模式實現引起的,那就不使用代理模式不就解決了嗎

使用

  1. 切換為代理模式
@EnableAsync(mode = AdviceMode.ASPECTJ)
  1. 添加aspectj織入包依賴
 <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-instrument -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

  1. 使用
 public void testAsync() {
        System.out.println(Thread.currentThread().getName());
        System.out.println("async1");
        testAsnc2();
    }

    /**
     * 測試ASPECTJ調用
     */
    @Async
    private void testAsnc2() {
        System.out.println(Thread.currentThread().getName());
        System.out.println("async2");
    }

  1. 啟動方式
    AspectJ是編譯器將切面織入到目標class的,啟動的使用需要加上java agent的參數
-Dserver.port=1000 -javaagent:${classpath}\spring-instrument-4.2.5.RELEASE.jar  
-javaagent:${classpath}\aspectjweaver-1.8.8.jar

總結

方法 限制
自調用 代理模式的限制,比如只能作用於public ,非static的方法
AopContext.currentProxy() 1. 代理模式的限制 2.ThreadLocal的限制,不能跨線程了 3.bean設置的限制,比如@Async代理創建方式不同其他
AspectJ 無限制,使用起來麻煩一點

關注公眾號【方丈的寺院】,第一時間收到文章的更新,與方丈一起開始技術修行之路
在這里插入圖片描述

參考

http://blog.kezhuw.name/2017/08/31/spring-aspectj-load-time-weaving/

https://cloud.tencent.com/developer/article/1497700

https://frightanic.com/software-development/spring-proxy-self-invocation/

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/ProxyAsyncConfiguration.html#asyncAdvisor--

https://www.baeldung.com/spring-aop-vs-aspectj


免責聲明!

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



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