动态代理

简介: 动态代理

一.概念

动态代理是一种方便运行时动态构建代理、动态处理代理方法调用的机制

动态代理的方式:

  • JDK 自身提供的动态代理,利用反射机制
  • cglib 动态代理,基于 ASM

## 二.场景

  • 面向切面的编程(AOP)
  • RPC 调用

三.代理解决的问题

首先,它是一个代理机制。如果熟悉设计模式中的代理模式,我们会知道,代理可以看作是对调用目标的一个包装,这样我们对目标代码的调用不是直接发生的,而是通过代理完成。通过代理可以让调用者与实现者之间解耦。比如进行 RPC 调用,通过代理,可以提供更加友善的界面。还可以通过代理,可以做一个全局的拦截器。

四.使用

1.jdk 的动态代理

I.使用

publicinterfaceAnimal {

   voideat();

}

publicclassCatimplementsAnimal {

   @Override

   publicvoideat() {

       System.out.println("The cat is eating");

   }

}

public class Dog implements Animal {

   @Override

   public void eat() {

       System.out.println("The dog is eating");

   }

}

public class AnimalProxy implements InvocationHandler {

   private Object target; // 代理对象


   public Object getInstance(Object target) {

       this.target = target;

       // 取得代理对象

       return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);

   }


   @Override

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

       System.out.println("调用前");

       Object result = method.invoke(target, args); // 方法调用

       System.out.println("调用后");

       return result;

   }

}

public class Test {

   public static void main(String[] args) {

       // JDK 动态代理调用

       AnimalProxy proxy = new AnimalProxy();

       Animal dogProxy = (Animal) proxy.getInstance(new Dog());

       dogProxy.eat();

   }

}

  • JDK Proxy 是通过实现 InvocationHandler 接口来实现的
  • 通过 newProxyInstance 获取到代理类对象
  • 重写 invoke 方法,以便代理类调用
  • JDK Proxy 只能代理实现接口的类

II.为什么 JDK Proxy 只能代理实现接口的类?

查看 newProxyInstance 的源码

/**

* @param   loader the class loader to define the proxy class

* @param   interfaces the list of interfaces for the proxy class to implement

*/

@CallerSensitive

public static Object newProxyInstance(ClassLoader loader,

                                     Class<?>[] interfaces,

                                     InvocationHandler h)

 throws IllegalArgumentException

{

 Objects.requireNonNull(h);

       final Class<?>[] intfs = interfaces.clone();

       final SecurityManager sm = System.getSecurityManager();

       if (sm != null) {

           checkProxyAccess(Reflection.getCallerClass(), loader, intfs);

       }

       /*

        * Look up or generate the designated proxy class.

        */

       Class<?> cl = getProxyClass0(loader, intfs);

    //省略其他代码

}

private static Class<?> getProxyClass0(ClassLoader loader,

                                      Class<?>... interfaces) {

 if (interfaces.length > 65535) {

   throw new IllegalArgumentException("interface limit exceeded");

 }


 // If the proxy class defined by the given loader implementing

 // the given interfaces exists, this will simply return the cached copy; 缓存

 // otherwise, it will create the proxy class via the ProxyClassFactory  代理类工厂

 return proxyClassCache.get(loader, interfaces);

}

public V get(K key, P parameter) {

 Objects.requireNonNull(parameter);//校验接口列表是否为null

 expungeStaleEntries();

 Object cacheKey = CacheKey.valueOf(key, refQueue);

 // lazily install the 2nd level valuesMap for the particular cacheKey

 ConcurrentMap<Object, Supplier<V>> valuesMap = map.get(cacheKey);

 if (valuesMap == null) {

   ConcurrentMap<Object, Supplier<V>> oldValuesMap

     = map.putIfAbsent(cacheKey,

                       valuesMap = new ConcurrentHashMap<>());

   if (oldValuesMap != null) {

     valuesMap = oldValuesMap;

   }

 }

 // create subKey and retrieve the possible Supplier<V> stored by that

 // subKey from valuesMap

 Object subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter));

 Supplier<V> supplier = valuesMap.get(subKey);

 Factory factory = null;

 while (true) {

   if (supplier != null) {

     // supplier might be a Factory or a CacheValue<V> instance

     V value = supplier.get();

     if (value != null) {

       return value;

     }

   }

   // else no supplier in cache

   // or a supplier that returned null (could be a cleared CacheValue

   // or a Factory that wasn't successful in installing the CacheValue)

   // lazily construct a Factory

   if (factory == null) {//没有缓存的时候,工厂也不存在,需要通过接口列表信息创建Factory

     factory = new Factory(key, parameter, subKey, valuesMap);

   }

   if (supplier == null) {

     supplier = valuesMap.putIfAbsent(subKey, factory);

     if (supplier == null) {

       // successfully installed Factory

       supplier = factory;

     }

     // else retry with winning supplier

   } else {

     if (valuesMap.replace(subKey, supplier, factory)) {

       // successfully replaced

       // cleared CacheEntry / unsuccessful Factory

       // with our Factory

       supplier = factory;

     } else {

       // retry with current supplier

       supplier = valuesMap.get(subKey);

     }

   }

 }

}

  • loader:为类加载器,也就是 target.getClass().getClassLoader()
  • interfaces:接口代理类的接口实现列表

根据注释说明,这个问题的源头,在于 JDK Proxy 的源码设计。首先会判断接口列表集合是否为空,为空直接抛出异常,不为空,如果缓存中不存在且工厂类也不存在,会通过接口列表信息创建工厂类.

如果要执意动态代理,非接口实现类就会报错.

2.cglib 动态代理

JDK 动态代理机制只能代理实现了接口的类,Cglib 是针对类来实现代理的,他的原理是对指定的目标类生成一个子类,并覆盖其中方法实现增强,但因为采用的是继承,所以不能对 final 修饰的类进行代理。

I.引入包

<dependency>

   <groupId>cglib</groupId>

   <artifactId>cglib</artifactId>

   <version>3.2.9</version>

</dependency>

II.代码实现

public class Panda {

   public void eat() {

       System.out.println("The panda is eating");

   }

}

public class CglibProxy implements MethodInterceptor {

   private Object target; // 代理对象


   public Object getInstance(Object target) {

       this.target = target;

       Enhancer enhancer = new Enhancer();

       // 设置父类为实例类

       enhancer.setSuperclass(this.target.getClass());

       // 回调方法

       enhancer.setCallback(this);

       // 创建代理对象

       return enhancer.create();

   }


   @Override

   public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

       System.out.println("调用前");

       Object result = methodProxy.invokeSuper(o, objects); // 执行方法调用

       System.out.println("调用后");

       return result;

   }

}

public class Test {

   public static void main(String[] args) {

       // CGLIB 动态代理调用

       CglibProxy proxy = new CglibProxy();

       Panda panda = (Panda) proxy.getInstance(new Panda());

       panda.eat();

   }

}

  • cglib 的调用通过实现 MethodInterceptor 接口的 intercept 方法;
  • 调用 invokeSuper 进行动态代理的;
  • 可以直接对普通类进行动态代理;

五.对比

JDK Proxy 的优势:

  • 最小化依赖关系,减少依赖意味着简化开发和维护,JDK 本身的支持,更加可靠;
  • 平滑进行 JDK 版本升级,而字节码类库通常需要进行更新以保证在新版上能够使用;

Cglib 框架的优势:

  • 可调用普通类,不需要实现接口;
  • 高性能;
  • 内部复杂,需要借助 Cglib 包,底层是 asm 字节码插桩技术
相关文章
|
敏捷开发 弹性计算 中间件
平台即服务(PaaS):简化开发与部署的新篇章
【6月更文挑战第21天】PaaS简化了应用开发与部署,提供资源池化、自动化管理及丰富的开发工具,助力企业降低成本、提高效率和系统稳定性。它支持敏捷开发、加速产品上市,改善用户体验,并推动创新,成为现代软件开发的关键。
1655 2
|
存储 Ubuntu 安全
百度搜索:蓝易云【Ubuntu系统离线安装Telnet服务教程。】
确保在进行任何系统配置更改之前备份重要的数据,并在操作过程中小心谨慎,以免造成不必要的问题。
731 0
|
11月前
|
数据采集 Web App开发 安全
uTLS高危指纹漏洞曝光:代理流量易被精准识别,开发者与用户紧急规避指南
uTLS曝高危漏洞,X25519密钥重用及加密套件矛盾致指纹暴露,流量易被识别。影响代理、爬虫等应用,建议禁用相关套件或升级修复版本,加强混淆策略以应对日益精准的TLS指纹检测。
966 6
|
11月前
|
存储 人工智能 运维
真实案例复盘:从“三套烟囱”到 All in ES,这家企业如何砍掉 40%运维成本
某泛娱乐平台面临搜索架构复杂、成本高企难题,通过阿里云Elasticsearch实现日志、搜索、向量一体化重构。借助Serverless化与混合存储,成本降60%,运维统一,查询效率倍增,验证了“All in ES”极简架构在AI时代的高效与可扩展性。
639 1
|
11月前
|
缓存 数据可视化 算法
【LED室内定位系统仿真软件:基于Python的高性能可视化仿真平台】
本文介绍基于Python与PyQt6开发的LED室内定位系统仿真软件,支持可见光通信(VLC)场景下的交互式仿真、实时信号分析与轨迹记录。提供时域、频域可视化、多参数配置及数据导出功能,适用于教学、科研与算法验证。项目开源,便于二次开发与扩展。
483 6
|
数据采集 机器学习/深度学习 算法
Python实现LightGBM分类模型(LGBMClassifier算法)项目实战
Python实现LightGBM分类模型(LGBMClassifier算法)项目实战
|
编解码 算法
为什么受损的视频数据通常显示为绿色?为什么很多30帧/秒的视频实际都是29.976帧/秒?
视频编码采用YUV格式因其亮度与色度分离,利于压缩且兼容黑白显示;受损视频常显绿色因YUV转RGB时Y、U、V为0导致;30帧/秒视频实为29.976帧/秒源于NTSC标准适应彩色电视需求;H.264等标准中H无特定含义,H.264又名MPEG-4 AVC,是ITU-T与ISO/IEC MPEG合作成果。
|
安全 云栖大会 数据安全/隐私保护
云栖大会演讲回顾 | 云市场心选包销伙伴赖炳辉:包销心选,大有可为
导语:云市场心选包销伙伴赖炳辉为大家介绍网站建设的现状与市场分析,以及自己携手阿里云云市场的收获、心得。
2512 155
云栖大会演讲回顾 | 云市场心选包销伙伴赖炳辉:包销心选,大有可为
|
人工智能 机器人
Kimi仅用5秒钟就帮我抓取了5页文章素材
Kimi仅用5秒钟就帮我抓取了5页文章素材
675 3
|
存储 JSON JavaScript
使用JSONObject解析与生成JSON数据
使用JSONObject解析与生成JSON数据