参考:
JAVA 动态代理
Java动态代理的两种实现方法
Java 动态代理作用是什么?
//定义被代理的接口
public interface Calculator {
Integer add(Integer num1, Integer num2);
Integer minus(Integer num1, Integer num2);
}
//被代理类
public class CalculatorImpl implements Calculator {
@Override
public Integer add(Integer num1, Integer num2) {
int ret = num1 + num2;
System.out.println("in calculatorImpl, res: " + ret);
return ret;
}
@Override
public Integer minus(Integer num1, Integer num2) {
int ret = num1 - num2;
System.out.println("int calculatorImpl, res: " + ret);
return ret;
}
}
静态代理:
//静态代理类
public class StaticCalculatorProxy implements Calculator {
Calculator obj;
public StaticCalculatorProxy(Calculator obj) {
this.obj = obj;
}
@Override
public Integer add(Integer num1, Integer num2) {
System.out.println("in StaticCalculatorProxy, before invocation");
Integer ret = obj.add(num1, num2);//被代理
System.out.println("in StaticCalculatorProxy, after invocation");
return ret;
}
@Override
public Integer minus(Integer num1, Integer num2) {
System.out.println("in StaticCalculatorProxy, before invocation");
Integer ret = obj.minus(num1, num2);//被代理
System.out.println("in StaticCalculatorProxy, after invocation");
return ret;
}
//如果有很多的方法,就很麻烦了
}
静态代理的客户端:
public class Client {
public static void main(String[] args) {
//被代理类
CalculatorImpl calculatorImpl = new CalculatorImpl();
//静态代理类:代理一次new一次
StaticCalculatorProxy staticCalculatorProxy = new StaticCalculatorProxy(calculatorImpl);
System.out.println(staticCalculatorProxy.add(1,2));
System.out.println(staticCalculatorProxy.minus(1, 2));
}
}
动态代理:
jdk动态代理是由java内部的反射机制来实现的。
动态代理类需要实现InvocationHandler 接口
//动态代理类,实现InvocationHandler 接口
public class CalculatorHandler implements InvocationHandler {
private Object obj; //被代理类
//绑定委托对象,并返回代理类
public Object bind(Object obj) {
this.obj = obj;
//绑定该类实现的所有接口,取得代理类
return Proxy.newProxyInstance(
obj.getClass().getClassLoader(),
obj.getClass().getInterfaces(),
this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("in calculatorhandler, before invocation");//其他操作
Object ret = method.invoke(obj, args); //执行被代理类方法
System.out.println("in calculationhandler, after invocation");//其他操作
return ret;
}
}
动态代理的客户端:
public class Client {
public static void main(String[] args) {
CalculatorImpl calculatorImpl = new CalculatorImpl();//被代理类
CalculatorHandler calculatorHandler = new CalculatorHandler();
Calculator calculator = (Calculator) calculatorHandler.bind(calculatorImpl);//代理类
System.out.println(calculator.add(1,2));
System.out.println(calculator.minus(1, 2));
}
}
实战:ArrayList 的动态代理类:
/* ArrayList 的动态代理类*/
public class ProxyTest {
public static void main(String[] args) {
final ArrayList<String> list = new ArrayList<>();
InvocationHandler invocationHandler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(list, args);
}
};
ClassLoader classLoader = list.getClass().getClassLoader();
Class<?>[] interfaces = list.getClass().getInterfaces();
List<String> listProxy = (List<String>) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
listProxy.add("你好");
System.out.println(list);
}
}