Java AOP的底層實現原理

一、什麼是AOP

1、AOP:Aspect Oriented Programming(面向切面編程),OOP是面向對象編程,AOP是在OOP基礎之上的一種更高級的設計思想。

2、OOP和AOP之間也存在一些區別,OOP側重於對象的提取和封裝。----封裝對象

AOP側重於方面組件,方面組件可以理解成封裝了通用功能的組件,方面組件可以通過配置方式,靈活地切入到某一批目標對象方法上。----封裝功能

3、AOP用於處理系統中分佈於各個模塊的橫切關注點,比如事務管理、日誌、緩存等。

二、AOP代理

1.AOP實現的關鍵,在於AOP框架自動創建的AOP代理,AOP代理主要分爲靜態代理和動態代理,靜態代理的代表爲AspectJ;而動態代理則以Spring AOP爲代表。

2.使用AspectJ的編譯時增強,實現AOP。

之前提到,AspectJ是靜態代理的增強。所謂的靜態代理,就是AOP框架會在編譯階段生成AOP代理類,因此也稱爲編譯時增強。

3.舉個實例來說。首先我們有一個普通的Hello類:

public class Hello { 

    public void sayHello() { 
        System.out.println("hello"); 
    } 

    public static void main(String[] args) { 
        Hello h = new Hello(); 
        h.sayHello(); 
    }
}

4.使用AspectJ編寫一個Aspect:

public aspect TxAspect { 
    void around:call(void Hello.sayHello()) {
         System.out.println("開始事務。。。");
         proceed(); 
         System.out.println("結束事務。。。"); 
    } 
}

5.這裏模擬了一個事務的場景,類似於Spring的聲明式事務。使用AspectJ的編譯器編譯:

ajc -d . Hello.java TxAspect.aj

編譯完成之後,再運行Hello類,可以看到以下輸出:

1)開始事務。。。
2)hello
3)結束事務。。。

顯然,AOP已經生效了。這就是AspectJ的靜態代理,它會在編譯階段將Aspect織入Java字節碼中,運行的時候就是經過增強之後的AOP對象。proceed方法就是回調執行被代理類中的方法。

三、使用Spring AOP

1.與AspectJ的靜態代理不同,Spring AOP使用的是動態代理。所謂的動態代理,就是說AOP框架不會去修改字節碼,而是在內存中臨時爲方法生成一個AOP對象,這個AOP對象包含了目標對象的全部方法,並且在特定的切點做了增強處理,並回調原對象的方法。

2.Spring AOP中的動態代理,主要有兩種方式:JDK動態代理和CGLIB動態代理。JDK動態代理通過“反射”來接收被代理的類,並且要求被代理的類必須實現一個接口。JDK動態代理的核心是InvocationHandler接口和Proxy類。如果目標類沒有實現接口,那麼Spring AOP會選擇使用CGLIB來動態代理目標類。

3.CGLIB(Code Generation Library),是一個代碼生成的類庫,可以在運行時動態地生成某個類的子類。注意,CGLIB是通過繼承的方式做的動態代理,因此如果某個類被標記爲final,那麼它是無法使用CGLIB做動態代理的。

四、JDK動態代理

1.爲了驗證以上的說法,可以做一個簡單的測試。首先測試實現接口的情況。

2.定義一個接口:

public interface Person { 
    void sayHello(String name); 
}

3.實現類:


@Component 
public class Chinese implements Person { 

    @Timer 
    @Override 
    public void sayHello(String name) { 
        System.out.println("-- sayHello()--");  
    }; 

    public void eat(String food) { 
        System.out.println("我正在吃:" + food); 
    } 
}

這裏的@Timer註解,是自定義的一個普通註解,用來標記Pointcut。
4.定義Aspect:

@Aspect 
@Component 
public class AdviceTest { 

    @Pointcut("annotation(com.spring.aop.Timer)") 
    public void pointcut() {
 
    } 

    @Before("pointcut()") 
    public void before() {
        System.out.println("before"); 
    } 
}

 

5.運行:

@SpringBootApplication 
@RestController 
public class SpringBootDemoApplication { 

    // 這裏必須使用Person接口做注入 
    @Autowired 
    private Person chinese; 

    @RequestMapping("/test") 
    public void test() { 
        chinese.sayHello("listen"); 
        System.out.println(chinese.getClass()); 
    } 

    public static void main(String[] args) {                                         
        SpringApplication.run(SpringBootDemoApplication.class, args); 
    } 
}

 

6.輸出:

1)before
2)-- sayHello() --
3)class com.sun.proxy.$Proxy53

可以看到類型是class com.sun.proxy.$Proxy53,也就是前面提到的Proxy類,因此這裏Spring AOP使用了JDK的動態代理。

五、CGLIB動態代理

1.再來看看不實現接口的情況,修改Chinese類:

@Component
public class Chinese {

    @Timer
    public void sayHello(String name) {
        System.out.println("-- sayHello() --"); 
    };

    public void eat(String food) {
        System.out.println("我正在吃:" + food);
    }
}

2.定義Aspect。

3.運行:

@SpringBootApplication 
@RestController 
public class SpringBootDemoApplication { 

    // 直接用Chinese類注入 
    @Autowired private Chinese chinese; 
    @RequestMapping("/test") 
    public void test() { 
        chinese.sayHello("listen"); 
        System.out.println(chinese.getClass()); 
    } 

    public static void main(String[] args) {         
        SpringApplication.run(SpringBootDemoApplication.class, args); 
    } 
}

4.輸出:

1)before
2)-- sayHello() --
3)class com.spring.aop.Chinese

可以看到類被CGLIB增強了,也就是動態代理。這裏的CGLIB代理就是Spring AOP的代理,這個類也就是所謂的AOP代理,AOP代理類在切點動態地織入了增強處理。

發佈了15 篇原創文章 · 獲贊 2 · 訪問量 1900
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章