JDK动态代理
1、动态代理实现方式
- 基于接口实现动态代理:JDK动态代理
- 基于继承实现动态代理:Cglib、Javassist动态代理
2、JDK动态代理
2.1 主要的类介绍
java.lang.reflect.Proxy:是所有代理类的父类,用于生成代理类或者代理实例。
getProxyClass:获取代理类的Class对象。
newProxyInstance:获取代理类实例。
java.lang.reflect.InvocationHandler:完成动态代理的整个过程。
invoke:整个代理过程实现。
2.2 代码实现
第一种实现方式:newProxyInstance
接口Caculator
package com.mine.proxy;
public interface Caculator {
public int add(int i, int j);
public int sub(int i, int j);
public int mul(int i, int j);
public int div(int i, int j);
}
实现类CaculatorImpl
package com.mine.proxy;
public class CaculatorImpl implements Caculator {
public int add(int i, int j) {
System.out.println("add excute");
return i + j;
}
public int sub(int i, int j) {
System.out.println("sub excute");
return i - j;
}
public int mul(int i, int j) {
System.out.println("mul excute");
return i * j;
}
public int div(int i, int j) {
System.out.println("div excute");
return i / j;
}
}
代理CaculatorProxy
package com.mine.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class CaculatorProxy implements InvocationHandler {
// 目标对象
private Object target;
public CaculatorProxy(Object target) {
this.target = target;
}
// 获取代理对象
public Object getProxy() {
// 代理对象
Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
return proxy;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
System.out.println("invoke " + methodName + " before");
Object result = method.invoke(target, args);
System.out.println("invoke " + methodName + " after");
return result;
}
}
测试类Main
package com.mine.proxy;
public class Main {
public static void main(String[] args) {
// 目标对象
Caculator caculatorImpl = new CaculatorImpl();
// 获取代理对象
Caculator proxy = (Caculator)new CaculatorProxy(caculatorImpl).getProxy();
int result1 = proxy.add(1, 3);
System.out.println("main result1: " + result1);
int result2 = proxy.sub(5, 1);
System.out.println("main result2: " + result2);
}
}
第二种实现方式:newProxyInstance
代理类CaculatorProxy2
package com.mine.proxy;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class CaculatorProxy2 implements InvocationHandler {
// 目标对象
private Object target;
public CaculatorProxy2(Object target) {
this.target = target;
}
// 获取代理对象
public Object getProxy() throws Exception {
// 代理对象
Class<?> proxyClass = Proxy.getProxyClass(target.getClass().getClassLoader(), target.getClass().getInterfaces());
Constructor<?> constructor = proxyClass.getConstructor(InvocationHandler.class);
Object proxy = constructor.newInstance(this);
return proxy;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
System.out.println("CaculatorProxy2.invoke " + methodName + " before");
Object result = method.invoke(target, args);
System.out.println("CaculatorProxy2.invoke " + methodName + " after");
return result;
}
public static void main(String[] args) throws Exception {
// 目标对象
Caculator caculatorImpl = new CaculatorImpl();
// 获取代理对象
Caculator proxy = (Caculator)new CaculatorProxy2(caculatorImpl).getProxy();
int result1 = proxy.add(1, 3);
System.out.println("main result1: " + result1);
int result2 = proxy.sub(5, 1);
System.out.println("main result2: " + result2);
}
}