Java静态代理和动态代理的使用及原理解析

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;

}

}

调用者:

public class ProxyTest {

public static void main(String[] args) {

Operator operator = new Operator();

MethodInterceptorImpl methodInterceptorImpl = new MethodInterceptorImpl();

//初始化加强器对象

Enhancer enhancer = new Enhancer();

//设置代理类

enhancer.setSuperclass(operator.getClass());

//设置代理回调

enhancer.setCallback(methodInterceptorImpl);

//创建代理对象

Operator operationProxy = (Operator) enhancer.create();

//调用操作方法

operationProxy.doSomething();

}

}

使用 CGLIB 进行动态代理的过程分为四个 需要zi料+ 绿色徽【vip1024b】

步骤:

  • 使用 MethodInterceptorImpl 实现 MethodInterceptor 接口,并在 intercept 方法中进行额外的操作

  • 创建增强器 Enhance 并设置被代理的操作类

  • 生成代理类

  • 调用代理对象的操作方法

总结

总结

在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

并设置被代理的操作类

  • 生成代理类
  • 调用代理对象的操作方法

总结

总结

在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。

[外链图片转存中…(img-VJLKRmjM-1710358341060)]

[外链图片转存中…(img-5QJdDWMn-1710358341060)]

[外链图片转存中…(img-myjq41ft-1710358341061)]

[外链图片转存中…(img-GPKt3cJX-1710358341061)]

  • 19
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值