JDK proxy 和cglib 源码解读
1 JDK Proxy 案例
JDK 代理类必须要实现一个接口。
创建接口
package com.gientech.proxy.jdk;
public interface ICalculator {
public Integer add(Integer i,Integer j);
public Integer sub(Integer i,Integer j);
public Integer mul(Integer i,Integer j);
public Integer div(Integer i,Integer j);
}
创建实现类
package com.gientech.proxy.jdk;
public class MyCalculator implements ICalculator{
@Override
public Integer add(Integer i, Integer j) {
return i+j;
}
@Override
public Integer sub(Integer i, Integer j) {
return i-j;
}
@Override
public Integer mul(Integer i, Integer j) {
return i*j;
}
@Override
public Integer div(Integer i, Integer j) {
return i/j;
}
}
创建proxy
package com.gientech.proxy.jdk;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class CalculatorProxy {
public static ICalculator getProxy(final ICalculator calculator){
ClassLoader loader = calculator.getClass().getClassLoader();
Class<?>[] interfaces = calculator.getClass().getInterfaces();
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = null;
try {
result = method.invoke(calculator, args);
}catch (Exception e){
System.out.println(e);
}finally {
}
return result;
}
};
Object proxy = Proxy.newProxyInstance(loader, interfaces, handler);
return (ICalculator) proxy;
}
}
创建测试类
package com.gientech.proxy.jdk;
public class JDKProxyTest {
public static void main(String[] args) {
System.getProperties().put("sun.misc.", "true");
ICalculator proxy = CalculatorProxy.getProxy(new MyCalculator());
Integer result = proxy.add(1,1);
System.out.println("result ---- " + result);
System.out.println(proxy.getClass());
}
}
运行结果如下:
2 Cglib proxy 案例
Classload
创建被代理类
package com.gientech.proxy.cglib;
public class MyCalculator {
public Integer add(Integer i, Integer j) {
return i+j;
}
public Integer sub(Integer i, Integer j) {
return i-j;
}
public Integer mul(Integer i, Integer j) {
return i*j;
}
public Integer div(Integer i, Integer j) {
return i/j;
}
}
创建回调对象
package com.gientech.proxy.cglib;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class MyCglib implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
Object o1 = methodProxy.invokeSuper(o, objects);
return o1;
}
}
创建测试类
package com.gientech.proxy.cglib;
import org.springframework.cglib.core.DebuggingClassWriter;
import org.springframework.cglib.proxy.Enhancer;
public class MyCglibTest {
public static void main(String[] args) {
// 动态代理创建的class文件村存储到本地
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "d:\\test\\code");
// 通过cglib动态代理获取代理对象的过程,创建调用的对象
Enhancer enhancer = new Enhancer();
// 设置enhancer对象的父类
enhancer.setSuperclass(MyCalculator.class);
// 设置enhancer的回调对象
enhancer.setCallback(new MyCglib());
// 创建代理对象
MyCalculator myCalculator = (MyCalculator) enhancer.create();
// 通过代理对象调用目标方法
int result = myCalculator.add(1,2);
System.out.println(myCalculator.getClass() + " --- " + result);
}
}