动态代理玩不明白?别紧张,你只是缺少这个demo
关注我 , 一个脑回路氢气的程序员 , 总有一些神奇的想法 , 最近有问题的老铁 , 可以下方评论区回复我或者私信我 , 想要学习什么知识 , 大家一起努力
今天我们来看一下动态代理
概念
JDK动态代理与静态代理是相对的 , 静态代理是在编译期就确定了代理类和被代理类的关系 , 且生成代理类 。 而且静态代理要求被代理的接口必须有其实现 , 因为代理类实现接口方法实际上是调用接口实现类的方法实现的 。 而动态代理是在运行期利用JVM的反射机制生成代理类,这里是直接生成类的字节码 , 然后通过类加载器载入JAVA虚拟机执行 。
demo
不做赘述 , 直接上demo 。
被代理类接口
public interface ISubject {void doSomething();}
被代理类实现
public class SubjectImpl implements ISubject {@Overridepublic void doSomething() {System.out.println("doSomething...");}}
InvocationHandler工具类
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;public class JDKDynamicProxy implements InvocationHandler{?private Object target;?public JDKDynamicProxy(){}?public JDKDynamicProxy(Object target){this.target=target;}?public T getProxy(){System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");// return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);}?@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println("before...");Object result=method.invoke(target,args);System.out.println("after...");return result;}}
测试类
【动态代理玩不明白?别紧张,你只是缺少这个demo】public class TestProxy {?public static void main(String[] args) {System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles","true");?ISubject subject=new JDKDynamicProxy(new SubjectImpl()).getProxy();//ISubject subject= (ISubject) Proxy.newProxyInstance(subjectImpl.getClass().getClassLoader(),subjectImpl.getClass().getInterfaces(),new JDKDynamicProxy(subjectImpl));//ISubject subject= (ISubject) Proxy.newProxyInstance(ISubject.class.getClassLoader(),new Class[]{ISubject.class},new JDKDynamicProxy(subjectImpl));subject.doSomething();}}
运行结果
before...doSomething...after...
生成的代理类文件
//// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)//?package com.sun.proxy;?import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.lang.reflect.UndeclaredThrowableException;?public final class $Proxy0 extends Proxy implements ISubject {private static Method m1;private static Method m3;private static Method m2;private static Method m0;?public $Proxy0(InvocationHandler var1) throws {super(var1);}?public final boolean equals(Object var1) throws {try {return (Boolean)super.h.invoke(this, m1, new Object[]{var1});} catch (RuntimeException | Error var3) {throw var3;} catch (Throwable var4) {throw new UndeclaredThrowableException(var4);}}?public final void doSomething() throws {try {super.h.invoke(this, m3, (Object[])null);} catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);}}?public final String toString() throws {try {return (String)super.h.invoke(this, m2, (Object[])null);} catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);}}?public final int hashCode() throws {try {return (Integer)super.h.invoke(this, m0, (Object[])null);} catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);}}?static {try {m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));m3 = Class.forName("ISubject").getMethod("doSomething");m2 = Class.forName("java.lang.Object").getMethod("toString");m0 = Class.forName("java.lang.Object").getMethod("hashCode");} catch (NoSuchMethodException var2) {throw new NoSuchMethodError(var2.getMessage());} catch (ClassNotFoundException var3) {throw new NoClassDefFoundError(var3.getMessage());}}}
- 设计模式6之代理模式
- 对微前端的11个错误认识
- 刷爆全网的动态条形图,只需5行Python代码就能实现
- 汕头世爵影音获挪威音乐之旅品牌中国(包括香港,澳门)总代理
- 从零并发框架(三)异步转同步注解+字节码增强代理实现
- HashiCorp开源Boundary:安全访问动态主机服务
- Opera GX游戏浏览器现已支持Razer Chroma RGB动态灯效
- 一位二十年老烧的经验之谈(二)
- 全息行业动态|科技让你我不再有距离
- 使用nginx反向代理github