• 动态代理proxy


    动态代理proxy

    1. People 接口
    package com.bors.proxy;
    
    public interface People {
    
        public void findWife();
    }
    2. XiaoMing implements People
    package com.bors.proxy;
    
    public class XiaoMing implements People{
    
        //结婚之前,需要小明父母帮忙找对象
    
        @Override
        public void findWife() {
            System.out.println("我是小明,我是个单身狗,我想找个女朋友娶回家!");
        }
    
        //结婚后,需要小明父母帮忙带孩子
    }

    3. Parent implements InvocationHandler

    package com.bors.proxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    
    // 增强,advice
    public class Parent implements InvocationHandler {
        private People people;
    
        public Parent(People people) {
            this.people = people;
        }
    
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            before();
    
            method.invoke(people, args);
    
            after();
            return null;
        }
    
        // 封版方法findWife之前执行的方法
        private void before() {
            System.out.println("====================我们是小明父母,我们拿着小明的个人介绍,帮小明找对象");
        }
    
        // 封版方法findWife之后执行的方法
        private void after() {
            System.out.println("====================我们是小明父母,我们在小明结婚后帮小明带孩子");
        }
    
    }

     4. Test 

    public class AppTest {
    
        @Test
        public void Test1() {
            // proxy代理实例,是对被代理对象起增强作用
            People proxyobj = (People) Proxy.newProxyInstance(Test.class.getClassLoader(),
                    new Class<?>[]{People.class}, new Parent(new XiaoMing()));
    
            createProxyClassFile();
    
            proxyobj.findWife();
        }
    
        // 生成代理实例$Proxy2的字节码文件
        public static void createProxyClassFile() {
            byte[] $Proxy2s = ProxyGenerator.generateProxyClass("$Proxy2", new Class[]{People.class});
    
            try {
                FileOutputStream fileOutputStream = new FileOutputStream("$Proxy2.class");
                fileOutputStream.write($Proxy2s);
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    5. $Proxy2 extends Proxy implements People 

    public final class $Proxy2 extends Proxy implements People {
        private static Method m1;
        private static Method m2;
        private static Method m3;
        private static Method m0;
    
        public $Proxy2(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 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 void findWife() throws  {
            try {
                super.h.invoke(this, m3, (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"));
                m2 = Class.forName("java.lang.Object").getMethod("toString");
                m3 = Class.forName("com.bors.proxy.People").getMethod("findWife");
                m0 = Class.forName("java.lang.Object").getMethod("hashCode");
            } catch (NoSuchMethodException var2) {
                throw new NoSuchMethodError(var2.getMessage());
            } catch (ClassNotFoundException var3) {
                throw new NoClassDefFoundError(var3.getMessage());
            }
        }
    }

     

     重点:

    1、InvocationHandler里面的invoke方法是被谁调用的??

     

    2、Proxy.newProxyInstance()执行过程



  • 相关阅读:
    BAT 大厂最流行的测试技术体系与测试职业发展晋级指南
    通知 | 2021 第一期《测试开发实战宝典》赠书活动顺利结束!
    你还缺个好工作?但7月毕业季即将来临怎么办
    一图看懂云栖大会「云原生」重磅发布
    阿里云重磅发布业务中台产品 BizWorks,中台发展进入下一个阶段
    云栖·追踪云原生|Serverless入围阿里云十大核心技术
    先行一步,7大技术创新和突破,阿里云把 Serverless 领域的这些难题都给解了
    New homework
    German
    suggestion
  • 原文地址:https://www.cnblogs.com/bors/p/proxy.html
Copyright © 2020-2023  润新知