查看 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, "保存路径");
原文地址:https://www.cnblogs.com/laoxia/p/11115593.html