【Java深入研究】6、CGLib動態代理機制詳解,深入研究cglib

來源:互聯網
上載者:User

【Java深入研究】6、CGLib動態代理機制詳解,深入研究cglib

一、首先說一下JDK中的動態代理:

JDK中的動態代理是通過反射類Proxy以及InvocationHandler回調介面實現的

但是,JDK中所要進行動態代理的類必須要實現一個介面,也就是說只能對該類所實現介面中定義的方法進行代理,這在實際編程中具有一定的局限性,而且使用反射的效率也並不是很高。

二、使用CGLib實現:

使用CGLib實現動態代理,完全不受代理類必須實現介面的限制,而且CGLib底層採用ASM位元組碼產生架構,使用位元組碼技術組建代理程式類,比使用Java反射效率要高。唯一需要注意的是,CGLib不能對聲明為final的方法進行代理,因為CGLib原理是動態產生被代理類的子類。

下面,將通過一個執行個體介紹使用CGLib實現動態代理。

1、被代理類:

首先,定義一個類,該類沒有實現任何介面,包含兩個方法。

Java代碼  
  1. public class ConcreteClassNoInterface {  
  2.     public String getConcreteMethodA(String str){  
  3.         System.out.println("ConcreteMethod A ... "+str);  
  4.         return str;  
  5.     }  
  6.     public int getConcreteMethodB(int n){  
  7.         System.out.println("ConcreteMethod B ... "+n);  
  8.         return n+10;  
  9.     }  
  10. }  

2、攔截器:

定義一個攔截器。在調用目標方法時,CGLib會回調MethodInterceptor介面方法攔截,來實現你自己的代理邏輯,類似於JDK中的InvocationHandler介面。

Java代碼  
  1. public class ConcreteClassInterceptor implements MethodInterceptor{  
  2.     public Object intercept(Object obj, Method method, Object[] arg, MethodProxy proxy) throws Throwable {  
  3.         System.out.println("Before:"+method);    
  4.         Object object=proxy.invokeSuper(obj, arg);  
  5.         System.out.println("After:"+method);   
  6.         return object;  
  7.     }  
  8. }  

參數:Object為由CGLib動態產生的代理類執行個體,Method為上文中實體類所調用的被代理的方法引用,Object[]為參數值列表,MethodProxy為產生的代理類對方法的代理引用。

返回:從代理執行個體的方法調用返回的值。

其中,proxy.invokeSuper(obj,arg):

調用代理類執行個體上的proxy方法的父類方法(即實體類ConcreteClassNoInterface中對應的方法)

在這個樣本中,只在調用被代理類方法前後各列印了一句話,當然實際編程中可以是其它複雜邏輯。

3、產生動態代理類:

Java代碼  
  1. Enhancer enhancer=new Enhancer();  
  2. enhancer.setSuperclass(ConcreteClassNoInterface.class);  
  3. enhancer.setCallback(new ConcreteClassInterceptor());  
  4. ConcreteClassNoInterface ccni=(ConcreteClassNoInterface)enhancer.create();  

這裡Enhancer類是CGLib中的一個位元組碼增強器,它可以方便的對你想要處理的類進行擴充,以後會經常看到它。

首先將被代理類ConcreteClassNoInterface設定成父類,然後設定攔截器ConcreteClassInterceptor,最後執行enhancer.create()動態產生一個代理類,並從Object強制轉型成父類型ConcreteClassNoInterface。

最後,在代理類上調用方法:

Java代碼  
  1. ccni.getConcreteMethodA("shensy");  
  2. ccni.getConcreteMethodB(0);  

查看控制台輸出:

控制台代碼  
  1. Before :public java.lang.String generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodA(java.lang.String)  
  2. ConcreteMethod A ... shensy  
  3. After :public java.lang.String generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodA(java.lang.String)  
  4. Before :public int generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodB(int)  
  5. ConcreteMethod B ... 0  
  6. After :public int generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodB(int)  

可以看到,攔截器在調用被代理類方法前後都執行了print操作。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.