动态代理涉及到的5个类:1. 接口Interface 2.被代理类 3.动态代理处理类 4.测试类 5.生成的代理类
整个过程就是:先调用Proxy.newProxyInstance()方法,传入接口,class loader以及动态代理处理类(被代理类是处理类的一个属性,作为参数传入),这个方法生成接口的一个实现类(代理类),这个代理类继承自Proxy类,所以可以得到传入到Proxy类的动态代理处理类,代理类还实现了接口的所有方法,每个方法体就是去调用动态代理处理类的invoke方法,把方法体里被调用时传来的参数再传到invoke方法里去,这样就实现了动态代理~
1. 接口Interface
package proxy; public interface Interface { void doSomething(); void somethingElse(String args); }
2.被代理类
package proxy; public class RealObject implements Interface { @Override public void doSomething() { // TODO Auto-generated method stub System.out.println("realObject do something"); } @Override public void somethingElse(String args) { // TODO Auto-generated method stub System.out.println("realObject something else ,args:" + args); } }
package proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class ProxyHandler implements InvocationHandler { private RealObject realObj; public ProxyHandler(RealObject realObj) { // TODO Auto-generated constructor stub this.realObj = realObj; } @Override public Object invoke(Object arg0, Method arg1, Object[] arg2) throws Throwable { // TODO Auto-generated method stub System.out.println("before proxy"); arg1.invoke(realObj, arg2); System.out.println("after proxy"); return null; } }
package proxy; import java.lang.reflect.Proxy; public class ProxyTest { public static void main(String[] args) { RealObject realObj = new RealObject(); Interface proxy = (Interface) Proxy.newProxyInstance( Interface.class.getClassLoader(), new Class[] { Interface.class }, new ProxyHandler(realObj)); System.out.println(proxy.getClass()); proxy.doSomething(); proxy.somethingElse("dayna"); // // byte[] proxyClassFile = ProxyGenerator.generateProxyClass( // "DaynaProxy", new Class[] { Interface.class }); } }
import com.pactera.oscar.fwapp2.pqs.eb.view.Interface; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.lang.reflect.UndeclaredThrowableException; public final class DaynaProxy extends Proxy implements Interface { private static Method m1; private static Method m0; private static Method m3; private static Method m4; private static Method m2; public DaynaProxy(InvocationHandler paramInvocationHandler) throws { super(paramInvocationHandler); } public final boolean equals(Object paramObject) throws { try { return ((Boolean)this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue(); } catch (RuntimeException localRuntimeException) { throw localRuntimeException; } catch (Throwable localThrowable) { } throw new UndeclaredThrowableException(localThrowable); } public final int hashCode() throws { try { return ((Integer)this.h.invoke(this, m0, null)).intValue(); } catch (RuntimeException localRuntimeException) { throw localRuntimeException; } catch (Throwable localThrowable) { } throw new UndeclaredThrowableException(localThrowable); } public final void doSomething() throws { try { this.h.invoke(this, m3, null); return; } catch (RuntimeException localRuntimeException) { throw localRuntimeException; } catch (Throwable localThrowable) { } throw new UndeclaredThrowableException(localThrowable); } public final void somethingElse(String paramString) throws { try { this.h.invoke(this, m4, new Object[] { paramString }); return; } catch (RuntimeException localRuntimeException) { throw localRuntimeException; } catch (Throwable localThrowable) { } throw new UndeclaredThrowableException(localThrowable); } public final String toString() throws { try { return (String)this.h.invoke(this, m2, null); } catch (RuntimeException localRuntimeException) { throw localRuntimeException; } catch (Throwable localThrowable) { } throw new UndeclaredThrowableException(localThrowable); } static { try { m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") }); m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]); m3 = Class.forName("com.pactera.oscar.fwapp2.pqs.eb.view.Interface").getMethod("doSomething", new Class[0]); m4 = Class.forName("com.pactera.oscar.fwapp2.pqs.eb.view.Interface").getMethod("somethingElse", new Class[] { Class.forName("java.lang.String") }); m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]); return; } catch (NoSuchMethodException localNoSuchMethodException) { throw new NoSuchMethodError(localNoSuchMethodException.getMessage()); } catch (ClassNotFoundException localClassNotFoundException) { } throw new NoClassDefFoundError(localClassNotFoundException.getMessage()); } }
原文地址:http://blog.csdn.net/gaga704/article/details/46647579