//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.guanpj;
public class AutoGenerateClass {
public void show() {
System.out.println(“I’m just test generate .class file by javassit…”);
}
public AutoGenerateClass() {
}
}
可以看到,javassit 生成的类中,除了 show() 方法之外还默认生成了一个无参的构造方法。
自定义类加载器加载
为了能够让自定的类被加载出来,我们自定义了一个类加载器来加载指定的 .class 文件:
public class CustomClassLoader extends ClassLoader {
public CustomClassLoader() {
}
protected Class<?> findClass(String className) {
String path = “D://temp//” + className.replace(“.”,“//”) + “.class”;
byte[] classData = getClassData(path);
return defineClass(className, classData, 0, classData.length);
}
private byte[] getClassData(String path) {
try {
InputStream ins = new FileInputStream(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bufferSize = 4096;
byte[] buffer = new byte[bufferSize];
int bytesNumRead = 0;
while ((bytesNumRead = ins.read(buffer)) != -1) {
baos.write(buffer, 0, bytesNumRead);
}
return baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
接着,用 ClassLoader 加载刚才生成的 .class 文件:
public class TestLoadClass {
public static void main(String[] args) throws Exception {
CustomClassLoader classLoader = new CustomClassLoader();
Class clazz = classLoader.findClass(“com.guanpj.AutoGenerateClass”);
Object object = clazz.newInstance();
Method showMethod = clazz.getMethod(“show”, null);
showMethod.invoke(object, null);
}
}
后台输出如下:
成功执行了 show 方法!
利用 JDK 中的 Proxy 类进行动态代理
使用动态代理的初衷是简化代码,不管是 ASM 还是 Javassist,在进行动态代理的时候操作还是不够简便,这也违背了我们的初衷。我们来看一下怎么 InvocationHandler 怎么做:
InvocationHandler:
public class InvocationHandlerImpl implements InvocationHandler {
Operate operate;
//注入操作者对象
public InvocationHandlerImpl(Operate operate) {
this.operate = operate;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before calling method: " + method.getName());
//调用操纵者的具体操作方法
method.invoke(operate, args);
System.out.println("after calling method: " + method.getName());
return null;
}
}
调用者:
public class DynamicProxyTest {
public static void main(String[] args) {
//实例化操作者
Operate operate = new Operator();
//将操作者对象进行注入
InvocationHandlerImpl handler = new InvocationHandlerImpl(operate);
//生成代理对象
Operate operationProxy = (Operate) Proxy.newProxyInstance(operate.getClass().getClassLoader(),
operate.getClass().getInterfaces(), handler);
//调用操作方法
operationProxy.doSomething();
}
}
跟静态代理不同的是,动态代理的过程主要分为三个步骤
-
将操作者对象注入 InvocationHandlerImpl 类中。
-
将 InvocationHandlerImpl 对象注入 Proxy 类中并返回代理者对象,并在 invoke 方法中进行额外的操作
-
调用代理对象的操作方法
利用 CGLIB 进行动态代理
用 Proxy 类生成代理类的方法为 newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) ,第二个参数是操作者的接口数组,意味着只能代理它实现的接口里的方法,对于本来在操作者类中定义的方法表示无能为力,CGLIB(Code Generation Library) 解决了这个问题。
MethodInterceptorImpl:
public class MethodInterceptorImpl implements MethodInterceptor {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println(“before calling method:” + method.getName());
proxy.invokeSuper(obj, args);
System.out.println(“after calling method:” + method.getName());
return null;
}
}
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
分享
这次面试我也做了一些总结,确实还有很多要学的东西。相关面试题也做了整理,可以分享给大家,了解一下面试真题,想进大厂的或者想跳槽的小伙伴不妨好好利用时间来学习。学习的脚步一定不能停止!
Spring Cloud实战
Spring Boot实战
面试题整理(性能优化+微服务+并发编程+开源框架+分布式)
6280708)]
分享
这次面试我也做了一些总结,确实还有很多要学的东西。相关面试题也做了整理,可以分享给大家,了解一下面试真题,想进大厂的或者想跳槽的小伙伴不妨好好利用时间来学习。学习的脚步一定不能停止!
[外链图片转存中…(img-EsosWto8-1711056280708)]
Spring Cloud实战
[外链图片转存中…(img-8RasSVYq-1711056280709)]
Spring Boot实战
[外链图片转存中…(img-jkchYcCD-1711056280709)]
面试题整理(性能优化+微服务+并发编程+开源框架+分布式)