zoukankan      html  css  js  c++  java
  • 查看 Proxy 的 $ProxyX.class文件

    方法一:

    在调用动态代理的main方法中加上

    System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");

    方法二:

     1 public static void main(String[] args) {
     2         byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy0", UserDaoImpl.class.getInterfaces());
     3         String path = "E:\$Proxy0.class";
     4         try(FileOutputStream fos = new FileOutputStream(path)) {
     5             fos.write(classFile);
     6             fos.flush();
     7             System.out.println("代理类class文件写入成功");
     8         } catch (Exception e) {
     9             System.out.println("写文件错误");
    10         }
    11 }

    这样在运行代码的时候就会在项目的根目录(或者指定目录)下生成 com.sun.proxy.$ProxyX.class 了,我们可以通过反编译来理解 Proxy 的处理过程。

    可以看到$ProxyX.class是从Proxy派生过来的:

    在实现对其业务方法调用时,是通过实现了InvocationHandler的业务类的invoke方法实现的,如下图:

    CGLib动态生成的Class文件如何保存到本地磁盘:

    在调用动态代理的main方法中加上:

    System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "保存路径");
  • 相关阅读:
    常用的标签分类
    css 实现动态二级菜单
    5大主流浏览器内核
    MySQL里面的子查询
    Algolia Search
    Nginx配置
    PHP中Abstract与Interface区别
    Shell 基本语法
    百度 echarts K线图使用
    php_soap扩展应用
  • 原文地址:https://www.cnblogs.com/laoxia/p/11115593.html
Copyright © 2011-2022 走看看