CGLIB介紹與原理(部分節選自網絡)
一、什麼是CGLIB?
CGLIB是一個功能強大,高性能的代碼生成包。它為沒有實現接口的類提供代理,為JDK的動態代理提供了很好的補充。通常可以使用Java的動態代理創建代理,但當要代理的類沒有實現接口或者為了更好的性能,CGLIB是一個好的選擇。
二、CGLIB原理
CGLIB原理:動態生成一個要代理類的子類,子類重寫要代理的類的所有不是final的方法。在子類中採用方法攔截的技術攔截所有父類方法的調用,順勢織入橫切邏輯。它比使用java反射的JDK動態代理要快。
CGLIB底層:使用字節碼處理框架ASM,來轉換字節碼並生成新的類。不鼓勵直接使用ASM,因為它要求你必須對JVM內部結構包括class文件的格式和指令集都很熟悉。
CGLIB缺點:對於final方法,無法進行代理。
三、CGLIB的應用
廣泛的被許多AOP的框架使用,例如Spring AOP和dynaop。Hibernate使用CGLIB來代理單端single-ended(多對一和一對一)關聯。
四、CGLIB的API
1、Jar包:
cglib-nodep-2.2.jar:使用nodep包不需要關聯asm的jar包,jar包內部包含asm的類.
cglib-2.2.jar:使用此jar包需要關聯asm的jar包,否則運行時報錯.
2、CGLIB類庫:
由於基本代碼很少,學起來有一定的困難,主要是缺少文檔和示例,這也是CGLIB的一個不足之處。
本系列使用的CGLIB版本是2.2。
net.sf.cglib.core:底層字節碼處理類,他們大部分與ASM有關係。
net.sf.cglib.transform:編譯期或運行期類和類文件的轉換
net.sf.cglib.proxy:實現創建代理和方法攔截器的類
net.sf.cglib.reflect:實現快速反射和C#風格代理的類
net.sf.cglib.util:集合排序等工具類
net.sf.cglib.beans:JavaBean相關的工具類
本篇介紹通過MethodInterceptor和Enhancer實現一個動態代理。
一、首先說一下JDK中的動態代理:
JDK中的動態代理是通過反射類Proxy以及InvocationHandler回調接口實現的,
但是,JDK中所要進行動態代理的類必須要實現一個接口,也就是說只能對該類所實現接口中定義的方法進行代理,這在實際編程中具有一定的局限性,而且使用反射的效率也並不是很高。
二、使用CGLib實現:
使用CGLib實現動態代理,完全不受代理類必須實現接口的限制,而且CGLib底層採用ASM字節碼生成框架,使用字節碼技術生成代理類,比使用Java反射效率要高。唯一需要注意的是,CGLib不能對聲明為final的方法進行代理,因為CGLib原理是動態生成被代理類的子類。
下面,將通過一個實例介紹使用CGLib實現動態代理。
1、被代理類:
首先,定義一個類,該類沒有實現任何接口<span >package com.zghw.cglib;
/**
* 沒有實現接口,需要CGlib動態代理的目標類
*
* @author zghw
*
*/
public class TargetObject {
public String method1(String paramName) {
return paramName;
}
public int method2(int count) {
return count;
}
public int method3(int count) {
return count;
}
@Override
public String toString() {
return "TargetObject []"+ getClass();
}
}</span>
2、攔截器:
定義一個攔截器。在調用目標方法時,CGLib會回調MethodInterceptor接口方法攔截,來實現你自己的代理邏輯,類似於JDK中的InvocationHandler接口。
<span >package com.zghw.cglib;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
/**
* 目標對像攔截器,實現MethodInterceptor
* @author zghw
*
*/
public class TargetInterceptor implements MethodInterceptor{
/**
* 重寫方法攔截在方法前和方法後加入業務
* Object obj為目標對像
* Method method為目標方法
* Object[] params 為參數,
* MethodProxy proxy CGlib方法代理對像
*/
@Override
public Object intercept(Object obj, Method method, Object[] params,
MethodProxy proxy) throws Throwable {
System.out.println("調用前");
Object result = proxy.invokeSuper(obj, params);
System.out.println(" 調用後"+result);
return result;
}
}
</span>
參數:Object為由CGLib動態生成的代理類實例,Method為上文中實體類所調用的被代理的方法引用,Object[]為參數值列表,MethodProxy為生成的代理類對方法的代理引用。
返回:從代理實例的方法調用返回的值。
其中,proxy.invokeSuper(obj,arg):
調用代理類實例上的proxy方法的父類方法(即實體類TargetObject中對應的方法)
在這個示例中,只在調用被代理類方法前後各打印了一句話,當然實際編程中可以是其它複雜邏輯。
3、生成動態代理類:<span >package com.zghw.cglib;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;
public class TestCglib {
public static void main(String args[]) {
Enhancer enhancer =new Enhancer();
enhancer.setSuperclass(TargetObject.class);
enhancer.setCallback(new TargetInterceptor());
TargetObject targetObject2=(TargetObject)enhancer.create();
System.out.println(targetObject2);
System.out.println(targetObject2.method1("mmm1"));
System.out.println(targetObject2.method2(100));
System.out.println(targetObject2.method3(200));
}
}
</span>
這裡Enhancer類是CGLib中的一個字節碼增強器,它可以方便的對你想要處理的類進行擴展,以後會經常看到它。
首先將被代理類TargetObject設置成父類,然後設置攔截器TargetInterceptor,最後執行enhancer.create()動態生成一個代理類,並從Object強制轉型成父類型TargetObject。
最後,在代理類上調用方法.
4、回調過濾器CallbackFilter
一、作用
在CGLib回調時可以設置對不同方法執行不同的回調邏輯,或者根本不執行回調。
在JDK動態代理中並沒有類似的功能,對InvocationHandler接口方法的調用對代理類內的所以方法都有效。
定義實現過濾器CallbackFilter接口的類:
<span >package com.zghw.cglib;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.CallbackFilter;
/**
* 回調方法過濾
* @author zghw
*
*/
public class TargetMethodCallbackFilter implements CallbackFilter {
/**
* 過濾方法
* 返回的值為數字,代表了Callback數組中的索引位置,要到用的Callback
*/
@Override
public int accept(Method method) {
if(method.getName().equals("method1")){
System.out.println("filter method1 ==0");
return 0;
}
if(method.getName().equals("method2")){
System.out.println("filter method2 ==1");
return 1;
}
if(method.getName().equals("method3")){
System.out.println("filter method3 ==2");
return 2;
}
return 0;
}
}
</span>
其中return值為被代理類的各個方法在回調數組Callback[]中的位置索引(見下文)。<span >package com.zghw.cglib;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;
public class TestCglib {
public static void main(String args[]) {
Enhancer enhancer =new Enhancer();
enhancer.setSuperclass(TargetObject.class);
CallbackFilter callbackFilter = new TargetMethodCallbackFilter();
/**
* (1)callback1:方法攔截器
(2)NoOp.INSTANCE:這個NoOp表示no operator,即什麼操作也不做,代理類直接調用被代理的方法不進行攔截。
(3)FixedValue:表示鎖定方法返回值,無論被代理類的方法返回什麼值,回調方法都返回固定值。
*/
Callback noopCb=NoOp.INSTANCE;
Callback callback1=new TargetInterceptor();
Callback fixedValue=new TargetResultFixed();
Callback[] cbarray=new Callback[]{callback1,noopCb,fixedValue};
//enhancer.setCallback(new TargetInterceptor());
enhancer.setCallbacks(cbarray);
enhancer.setCallbackFilter(callbackFilter);
TargetObject targetObject2=(TargetObject)enhancer.create();
System.out.println(targetObject2);
System.out.println(targetObject2.method1("mmm1"));
System.out.println(targetObject2.method2(100));
System.out.println(targetObject2.method3(100));
System.out.println(targetObject2.method3(200));
}
}
</span>
<span >package com.zghw.cglib;
import net.sf.cglib.proxy.FixedValue;
/**
* 表示鎖定方法返回值,無論被代理類的方法返回什麼值,回調方法都返回固定值。
* @author zghw
*
*/
public class TargetResultFixed implements FixedValue{
/**
* 該類實現FixedValue接口,同時鎖定回調值為999
* (整型,CallbackFilter中定義的使用FixedValue型回調的方法為getConcreteMethodFixedValue,該方法返回值為整型)。
*/
@Override
public Object loadObject() throws Exception {
System.out.println("鎖定結果");
Object obj = 999;
return obj;
}
}
</span>
5.延遲加載對像 一、作用:
說到延遲加載,應該經常接觸到,尤其是使用Hibernate的時候,本篇將通過一個實例分析延遲加載的實現方式。
LazyLoader接口繼承了Callback,因此也算是CGLib中的一種Callback類型。
另一種延遲加載接口Dispatcher。
Dispatcher接口同樣繼承於Callback,也是一種回調類型。
但是Dispatcher和LazyLoader的區別在於:LazyLoader只在第一次訪問延遲加載屬性時觸發代理類回調方法,而Dispatcher在每次訪問延遲加載屬性時都會觸發代理類回調方法。
二、示例:
首先定義一個實體類LoaderBean,該Bean內有一個需要延遲加載的屬性PropertyBean。
<span >package com.zghw.cglib;
import net.sf.cglib.proxy.Enhancer;
public class LazyBean {
private String name;
private int age;
private PropertyBean propertyBean;
private PropertyBean propertyBeanDispatcher;
public LazyBean(String name, int age) {
System.out.println("lazy bean init");
this.name = name;
this.age = age;
this.propertyBean = createPropertyBean();
this.propertyBeanDispatcher = createPropertyBeanDispatcher();
}
/**
* 只第一次懶加載
* @return
*/
private PropertyBean createPropertyBean() {
/**
* 使用cglib進行懶加載 對需要延遲加載的對象添加代理,在獲取該對像屬性時先通過代理類回調方法進行對像初始化。
* 在不需要加載該對像時,只要不去獲取該對像內屬性,該對象就不會被初始化了(在CGLib的實現中只要去訪問該對像內屬性的getter方法,
* 就會自動觸發代理類回調)。
*/
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(PropertyBean.class);
PropertyBean pb = (PropertyBean) enhancer.create(PropertyBean.class,
new ConcreteClassLazyLoader());
return pb;
}
/**
* 每次都懶加載
* @return
*/
private PropertyBean createPropertyBeanDispatcher() {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(PropertyBean.class);
PropertyBean pb = (PropertyBean) enhancer.create(PropertyBean.class,
new ConcreteClassDispatcher());
return pb;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public PropertyBean getPropertyBean() {
return propertyBean;
}
public void setPropertyBean(PropertyBean propertyBean) {
this.propertyBean = propertyBean;
}
public PropertyBean getPropertyBeanDispatcher() {
return propertyBeanDispatcher;
}
public void setPropertyBeanDispatcher(PropertyBean propertyBeanDispatcher) {
this.propertyBeanDispatcher = propertyBeanDispatcher;
}
@Override
public String toString() {
return "LazyBean [name=" + name + ", age=" + age + ", propertyBean="
+ propertyBean + "]";
}
}
</span>
<span >package com.zghw.cglib;
public class PropertyBean {
private String key;
private Object value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public String toString() {
return "PropertyBean [key=" + key + ", value=" + value + "]" +getClass();
}
}
</span>
<span >package com.zghw.cglib;
import net.sf.cglib.proxy.LazyLoader;
public class ConcreteClassLazyLoader implements LazyLoader {
/**
* 對需要延遲加載的對象添加代理,在獲取該對像屬性時先通過代理類回調方法進行對像初始化。
* 在不需要加載該對像時,只要不去獲取該對像內屬性,該對象就不會被初始化了(在CGLib的實現中只要去訪問該對像內屬性的getter方法,
* 就會自動觸發代理類回調)。
*/
@Override
public Object loadObject() throws Exception {
System.out.println("before lazyLoader...");
PropertyBean propertyBean = new PropertyBean();
propertyBean.setKey("zghw");
propertyBean.setValue(new TargetObject());
System.out.println("after lazyLoader...");
return propertyBean;
}
}
</span>
<span >package com.zghw.cglib;
import net.sf.cglib.proxy.Dispatcher;
public class ConcreteClassDispatcher implements Dispatcher{
@Override
public Object loadObject() throws Exception {
System.out.println("before Dispatcher...");
PropertyBean propertyBean = new PropertyBean();
propertyBean.setKey("xxx");
propertyBean.setValue(new TargetObject());
System.out.println("after Dispatcher...");
return propertyBean;
}
}
</span>
6.接口生成器InterfaceMaker 一、作用:
InterfaceMaker會動態生成一個接口,該接口包含指定類定義的所有方法。
二、示例:
<span >package com.zghw.cglib;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InterfaceMaker;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class TestInterfaceMaker {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
InterfaceMaker interfaceMaker =new InterfaceMaker();
//抽取某個類的方法生成接口方法
interfaceMaker.add(TargetObject.class);
Class<?> targetInterface=interfaceMaker.create();
for(Method method : targetInterface.getMethods()){
System.out.println(method.getName());
}
//接口代理並設置代理接口方法攔截
Object object = Enhancer.create(Object.class, new Class[]{targetInterface}, new MethodInterceptor(){
@Override
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy methodProxy) throws Throwable {
if(method.getName().equals("method1")){
System.out.println("filter method1 ");
return "mmmmmmmmm";
}
if(method.getName().equals("method2")){
System.out.println("filter method2 ");
return 1111111;
}
if(method.getName().equals("method3")){
System.out.println("filter method3 ");
return 3333;
}
return "default";
}});
Method targetMethod1=object.getClass().getMethod("method3",new Class[]{int.class});
int i=(int)targetMethod1.invoke(object, new Object[]{33});
Method targetMethod=object.getClass().getMethod("method1",new Class[]{String.class});
System.out.println(targetMethod.invoke(object, new Object[]{"sdfs"}));
}
}
</span>