【动态代理的两种实现方式】

动态代理的两种实现方式

在Java中,动态代理主要有两种实现方式:基于接口的动态代理和基于类的动态代理。基于接口的动态代理通过java.lang.reflect.Proxy类实现,而基于类的动态代理通过第三方库如CGLIB实现。

一 JDK动态代理

  1. 基于接口的动态代理
    首先,我们定义一个接口和它的实现类:
    `
// 
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;
    }
}
  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值