动态代理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()执行过程