CGLib动态代理

首先,需要明白CGLib是什么,

CGLIB是一个功能强大,高性能的代码生成包。它为没有实现接口的类提供代理,为JDK的动态代理提供了很好的补充。通常可以使用Java的动态代理创建代理,但当要代理的类没有实现接口或者为了更好的性能,CGLIB是一个好的选择。

然后,CGLib动态代理相对于JDK动态代理有哪些优势?
JDK动态代理的类必须实现一个接口,也就是说只能对该类所实现接口中定义的方法进行代理,所以局限性很高,而使用CGLib实现动态代理,完全不受代理类必须实现接口的限制,同时CGLib底层采用ASM生成代理类,相对于JDK动态代理的反射来说,效率要高许多。
唯一注意的是,CGLib代理不能代理声明为final的方法。

CGLib实现动态代理的原理:

动态生成一个要代理类的子类,子类重写要代理的类的所有不是final的方法。在子类中采用方法拦截的技术拦截所有父类方法的调用,顺势织入横切逻辑。它比使用java反射的JDK动态代理要快。CGLib被很多AOP框架所使用,如Spring的AOP和DYNAOP。

下面用实例来说明:

   1.定义一个被代理类

    public class ConcreteClassNoInterface {  
        public String getConcreteMethodA(String str){  
            System.out.println("ConcreteMethod A ... "+str);  
            return str;  
        }  
        public int getConcreteMethodB(int n){  
            System.out.println("ConcreteMethod B ... "+n);  
            return n+10;  
        }  
        public int getConcreteMethodFixedValue(int n){  
            System.out.println("getConcreteMethodFixedValue..."+n);  
            return n+10;  
        }  
    }  

2.定义一个拦截器,在调用目标方法时,CGLib会回调MethodInterceptor接口方法拦截,来实现你自己的代理逻辑,类似于JDK中的InvocationHandler接口:

public class ConcreteClassInterceptor implements MethodInterceptor{  
    public Object intercept(Object obj, Method method, Object[] arg, MethodProxy proxy) throws Throwable {  
        System.out.println("Before:"+method);    
        Object object=proxy.invokeSuper(obj, arg);  
        System.out.println("After:"+method);   
        return object;  
    }  
} 

3.定义实现过滤器CallbackFilter接口的类

    public class ConcreteClassCallbackFilter implements CallbackFilter{  
        public int accept(Method method) {  
            if("getConcreteMethodB".equals(method.getName())){  
                return 0;//Callback callbacks[0]  
            }else if("getConcreteMethodA".equals(method.getName())){  
                return 1;//Callback callbacks[1]  
            }else if("getConcreteMethodFixedValue".equals(method.getName())){  
                return 2;//Callback callbacks[2]  
            }  
            return 1;  
        }  
    }  

4.定义FixedValue对应的类

    public class ConcreteClassFixedValue implements FixedValue{  
        public Object loadObject() throws Exception {  
            System.out.println("ConcreteClassFixedValue loadObject ...");  
            Object object=999;  
            return object;  
        }  
    }  

5.生成代理类,进行测试:

public static void main(String [] args){
    Enhancer enhancer=new Enhancer();  
    enhancer.setSuperclass(ConcreteClassNoInterface.class);  
    CallbackFilter filter=new ConcreteClassCallbackFilter();  
    enhancer.setCallbackFilter(filter);  
  
    Callback interceptor=new ConcreteClassInterceptor();//(1)  
    Callback noOp=NoOp.INSTANCE;//(2)  
    Callback fixedValue=new ConcreteClassFixedValue();//(3)  
    Callback[] callbacks=new Callback[]{interceptor,noOp,fixedValue};  
    enhancer.setCallbacks(callbacks);  
    ConcreteClassNoInterface proxyObject= (ConcreteClassNoInterface)enhancer.create();  
    System.out.println("*** NoOp Callback ***");  
    proxyObject.getConcreteMethodA("abcde");  
  
    System.out.println("*** MethodInterceptor Callback ***");  
    proxyObject.getConcreteMethodB(1);  
  
    System.out.println("*** FixedValue Callback ***");  
    int fixed1=proxyObject.getConcreteMethodFixedValue(128);  
    System.out.println("fixedValue1:"+fixed1);  
    int fixed2=proxyObject.getConcreteMethodFixedValue(256);  
    System.out.println("fixedValue2:"+fixed2);
    }

输出结果:

*** NoOp Callback ***  
ConcreteMethod A ... abcde  
  
*** MethodInterceptor Callback ***  
Before :public int generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodB(int)  
ConcreteMethod B ... 1  
After :public int generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodB(int)  
ConcreteClassInterceptor end...  
  
*** FixedValue Callback ***  
ConcreteClassFixedValue loadObject ...  
fixedValue1:999  
ConcreteClassFixedValue loadObject ...  
fixedValue2:999  

 根据上述代码我们来分析一下:

生成代理类前,设置了CallbackFilter,上文中ConcreteClassCallbackFilter实现类的返回值对应Callback[]数组中的位置索引。此处包含了CGLib中的3种回调方式:

(1)MethodInterceptor:方法拦截器,上一篇文章中已经详细介绍过,此处不再赘述。

(2)NoOp.INSTANCE:这个NoOp表示no operator,即什么操作也不做,代理类直接调用被代理的方法不进行拦截。

(3)FixedValue:表示锁定方法返回值,无论被代理类的方法返回什么值,回调方法都返回固定值。

getConcreteMethodA对应CallbackFilter中定义的索引1,在Callback[]数组中使用的过滤为NoOp,因此直接执行了被代理方法。

getConcreteMethodB对应CallbackFilter中定义的索引0,在Callback[]数组中使用的过滤为MethodInterceptor,因此执行了方法拦截器进行拦截。

getConcreteMethodFixedValue对应CallbackFilter中定义的索引2,在Callback[]数组中使用的过滤为FixedValue,因此2次赋值128和256的调用其结果均被锁定为返回999。

原文地址:https://www.cnblogs.com/shilin000/p/4791210.html