被代理的类及其实现的接口
package proxy.demo;public interface ServiceDemo { /** * 返回姓名 */ void getName(); /** * 返回年龄 */ void getAge();}
package proxy.demo;public class ServiceDemoImpl implements ServiceDemo { @Override public void getName() { System.out.println("kevin"); } @Override public void getAge() { System.out.println("20"); }}
为ServiceDemoImpl动态创建代理类
首先编写InvocationHandler
package proxy.demo;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;public class MyInvocationHandler implements InvocationHandler { private Object target;//被代理的对象,通过构造函数传入 public MyInvocationHandler(Object target){ this.target = target; } @Override public 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; }}
创建代理类并调用
import proxy.demo.Demo2;import proxy.demo.MyInvocationHandler;import proxy.demo.ServiceDemo;import proxy.demo.ServiceDemoImpl;import sun.misc.ProxyGenerator;import java.io.FileOutputStream;import java.io.IOException;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Proxy;/** * Created with IntelliJ IDEA * * @Author: bit hup * @Date: 11/10 0010 * @Time: 18:17 */public class Demo { public static void main(String[] args) throws Exception { ServiceDemo serviceDemo = new ServiceDemoImpl(); //创建InvocationHandler,注入被代理类的实例 InvocationHandler invocationHandler = new MyInvocationHandler(serviceDemo); //获取代理类对象 ServiceDemo serviceDemoProxy = (ServiceDemo) Proxy.newProxyInstance(serviceDemo.getClass().getClassLoader(), serviceDemo.getClass().getInterfaces(),invocationHandler); //将动态创建的代理类写入到磁盘class文件中,然后反编译查看生成的代码 String path = "D://$Proxy0.class"; byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy0",serviceDemo.getClass().getInterfaces()); FileOutputStream out = null; try { out = new FileOutputStream(path); out.write(classFile); out.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } //调用代理类 serviceDemoProxy.getName(); serviceDemoProxy.getAge(); }}
生产的代理类,反编译后的代码
//// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)//import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.lang.reflect.UndeclaredThrowableException;import proxy.demo.ServiceDemo;public final class $Proxy0 extends Proxy implements ServiceDemo { private static Method m1; private static Method m3; private static Method m2; private static Method m4; 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})).booleanValue(); } catch (RuntimeException | Error var3) { throw var3; } catch (Throwable var4) { throw new UndeclaredThrowableException(var4); } } public final void getName() 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 void getAge() throws { try { super.h.invoke(this, m4, (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)).intValue(); } catch (RuntimeException | Error var2) { throw var2; } catch (Throwable var3) { throw new UndeclaredThrowableException(var3); } } static { try { m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[]{Class.forName("java.lang.Object")}); m3 = Class.forName("proxy.demo.ServiceDemo").getMethod("getName", new Class[0]); m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]); m4 = Class.forName("proxy.demo.ServiceDemo").getMethod("getAge", new Class[0]); m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]); } catch (NoSuchMethodException var2) { throw new NoSuchMethodError(var2.getMessage()); } catch (ClassNotFoundException var3) { throw new NoClassDefFoundError(var3.getMessage()); } }}
1.静态代理三种角色
(一)抽象角色:接口或抽象类 (二)被代理角色:实现或继承抽象角色 (三)代理角色:实现或继承抽象角色,包含被代理对象的引用