动态代理的两种实现方式
在Java中,动态代理主要有两种实现方式:基于接口的动态代理和基于类的动态代理。基于接口的动态代理通过java.lang.reflect.Proxy类实现,而基于类的动态代理通过第三方库如CGLIB实现。
一 JDK动态代理
- 基于接口的动态代理
首先,我们定义一个接口和它的实现类:
`
//
public interface MyInterface {
void doSomething();
}
public class MyInterfaceImpl implements MyInterface {
@Override
public void doSomething() {
System.out.println("Doing something...");
}
}
然后,我们使用Proxy类创建动态代理:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class DynamicProxyDemo {
public static void main(String[] args) {
// 创建接口实现类的实例
MyInterface myInterface = new MyInterfaceImpl();
// 创建InvocationHandler实例
InvocationHandler handler = new MyInvocationHandler(myInterface);
// 创建代理实例
MyInterface proxyInstance = (MyInterface) Proxy.newProxyInstance(
myInterface.getClass().getClassLoader(),
myInterface.getClass().getInterfaces(),
handler
);
// 调用代理实例的方法
proxyInstance.doSomething();
}
}
class MyInvocationHandler implements InvocationHandler {
private final Object target;
public MyInvocationHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Before method call");
Object result = method.invoke(target, args);
System.out.println("After method call");
return result;
}
}
二. 基于类的动态代理(使用CGLIB)
CGLIB是一个强大的动态代理库,可以代理类而不需要接口。
定义一个普通类:
public class MyClass {
public void doSomething() {
System.out.println("Doing something...");
}
}
使用CGLIB创建动态代理:
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class CGLIBProxyDemo {
public static void main(String[] args) {
// 创建Enhancer实例
Enhancer enhancer = new Enhancer();
// 设置父类为代理类
enhancer.setSuperclass(MyClass.class);
// 设置回调
enhancer.setCallback(new MyMethodInterceptor());
// 创建代理实例
MyClass proxy = (MyClass) enhancer.create();
// 调用代理实例的方法
proxy.doSomething();
}
}
class MyMethodInterceptor implements MethodInterceptor {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("Before method call");
Object result = proxy.invokeSuper(obj, args);
System.out.println("After method call");
return result;
}
}