彻底搞懂JDK动态代理

通过一张图,先了解什么是代理模式

image.png

可以看到这种模式可以在调用方与目标对象之间起到中介作用,从而保护目标对象,并可以扩展目标对象的功能(ps:这里可以想想符合一种什么设计原则

如果想了解CGLIB代理可以看我这篇文章:彻底搞懂CGLIB代理

JDK动态代理具体实现

JDK动态代理是基于接口的代理,只能对实现了接口的类进行代理

1.定义接口,实现动态代理的目标接口

//定义接口
public interface TargetInterFace {

    void targetMethod();
}
//接口实现
public class TargetObject implements TargetInterFace{

    @Override
    public void targetMethod() {
        System.out.println("/// "+this.getClass().getName()+" 执行");
    }
}

2.实现InvocationHandler接口,重写invoke方法

InvocationHandler是Java中的一个接口,是Java反射API的一部分,它用于动态创建接口的代理实例。

public class AopJdkInvocation implements InvocationHandler {

    private Object target;

    public AopJdkInvocation(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("/// "+this.getClass().getName()+" 执行前");

        method.invoke(this.target, args);

        System.out.println("/// "+this.getClass().getName()+" 执行后");

        return null;
    }
}

InvocationHandler的主要作用是将方法调用与具体的实现解耦,使得在代理对象中可以灵活地处理方法调用。

这个接口只有一个方法,即invoke()方法,它负责处理对代理对象的方法调用(当代理实例调用方法时,方法调用被编码分派到InvocationHandler的invoke方法)

通过使用InvocationHandler,可以更加灵活地扩展程序的功能(例如在调用前后添加额外的逻辑、对调用参数进行校验等),而不需要修改原有的代码

3.创建动态代理对象,将目标对象和InvocationHandler传递给Proxy.newProxyInstance方法。

Proxy类是JDK提供的一个工具类,用于创建代理对象。它提供了一个静态方法newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h),用于创建代理对象。该方法需要三个参数:

  • ClassLoader loader:指定代理对象的类加载器。
  • Class<?>[] interfaces:指定代理对象要实现的接口。
  • InvocationHandler h:指定实现了InvocationHandler接口的对象,用于处理方法调用。
public static void main(String[] args) {
    //目标对象
    TargetObject targetObject = new TargetObject();
    //建立代理与被代理对象的关系
    AopJdkInvocation aopInvocation=new AopJdkInvocation(targetObject);
    //生成代理对象
    TargetInterFace instance = (TargetInterFace) Proxy.newProxyInstance(targetObject.getClass().getClassLoader(), targetObject.getClass().getInterfaces(), aopInvocation);
    instance.targetMethod();
}

image.png

JDK动态代理原理

为什么没有显式调用invoke,但invoke方法确实被执行了?

image.png

通过Proxy.newProxyInstance方法为大家解释

public static Object newProxyInstance(ClassLoader loader,
                                      Class<?>[] interfaces,
                                      InvocationHandler h)
    throws IllegalArgumentException
{
    /**
    * 非核心代码省略
    */
    
    /*
     * 获取到代理接口的class
     */
    Class<?> cl = getProxyClass0(loader, intfs);
    
    /*
     * Invoke its constructor with the designated invocation handler.
     */
    try {
        /**
        * 用反射获取构造方法,然后创建代理类实例
        */
        final Constructor<?> cons = cl.getConstructor(constructorParams);
        final InvocationHandler ih = h;
        
      /**
        * 非核心代码省略
       */
        return cons.newInstance(new Object[]{h});
    } catch (IllegalAccessException|InstantiationException e) {
        throw new InternalError(e.toString(), e);
    }
}

通过getProxyClass 获取到代理接口的class($Proxy0)
image.png

通过Arthas反编译$Proxy0的源码,可以看到该类实现了目标接口

image.png
然后通过用反射获取构造方法,然后创建代理类实例。

代理对象的方法调用都是通过super.h.invoke(this,m1,(Object[])null)调用,其中的
super.h.invoke实际上是在创建代理的时候传递给Proxy.newProxyInstance的
代理对象,它继承InvocationHandler类,负责实际的调用处理逻辑。
代理对象invoke()接收到method、args等参数后,然后通过反射让被代理的对象执行目标方法

总结

JDK动态代理符合我们的开闭原则。

JDK动态代理主要面向接口,可以为接口中的所有方法提供代理,没有实现接口的类无法进行代理。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值