二、动态代理
Java中动态代理的实现,关键就是这两个东西:Proxy、InvocationHandler,下面从InvocationHandler接口中的invoke方法入手,简单说明一下Java如何实现动态代理的。
首先,invoke方法的完整形式如下:
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
method.invoke(obj, args);
return null;
}
A method invocation on a proxy instance through one of its proxy interfaces will be dispatched to the invoke method of the instance's invocation handler,
passing the proxy instance,a java.lang.reflect.Method object identifying the method that was invoked, and an array of type Object containing the arguments.
由此可以知道以上的猜测是正确的,同时也知道,proxy参数传递的即是代理类的实例。 为了方便说明,这里写一个简单的例子来实现动态代理。
//抽象角色(动态代理只能代理接口)
public interface Subject {
public void request();
}
//真实角色:实现了Subject的request()方法
public class RealSubject implements Subject{
public void request(){
System.out.println("From real subject.");
}
}
//实现了InvocationHandler
public class DynamicSubject implements InvocationHandler
{
private Object obj;//这是动态代理的好处,被封装的对象是Object类型,接受任意类型的对象
public DynamicSubject()
{
}
public DynamicSubject(Object obj)
{
this.obj = obj;
}
//这个方法不是我们显示的去调用
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
System.out.println("before calling " + method);
method.invoke(obj, args);
System.out.println("after calling " + method);
return null;
}
}
//客户端:生成代理实例,并调用了request()方法
public class Client {
public static void main(String[] args) throws Throwable{
// TODO Auto-generated method stub
Subject rs=new RealSubject();//这里指定被代理类
InvocationHandler ds=new DynamicSubject(rs);
Class> cls=rs.getClass();
//以下是一次性生成代理
Subject subject=(Subject) Proxy.newProxyInstance(
cls.getClassLoader(),cls.getInterfaces(), ds);
//这里可以通过运行结果证明subject是Proxy的一个实例,这个实例实现了Subject接口
System.out.println(subject instanceof Proxy);
//这里可以看出subject的Class类是$Proxy0,这个$Proxy0类继承了Proxy,实现了Subject接口
System.out.println("subject的Class类是:"+subject.getClass().toString());
System.out.print("subject中的属性有:");
Field[] field=subject.getClass().getDeclaredFields();
for(Field f:field){
System.out.print(f.getName()+", ");
}
System.out.print("\n"+"subject中的方法有:");
Method[] method=subject.getClass().getDeclaredMethods();
for(Method m:method){
System.out.print(m.getName()+", ");
}
System.out.println("\n"+"subject的父类是:"+subject.getClass().getSuperclass());
System.out.print("\n"+"subject实现的接口是:");
Class>[] interfaces=subject.getClass().getInterfaces();
for(Class> i:interfaces){
System.out.print(i.getName()+", ");
}
System.out.println("\n\n"+"运行结果为:");
subject.request();
}
}
运行结果如下:此处省略了包名,***代替
true
subject的Class类是:class $Proxy0
subject中的属性有:m1, m3, m0, m2,
subject中的方法有:request, hashCode, equals, toString,
subject的父类是:class java.lang.reflect.Proxy
subject实现的接口是:cn.edu.ustc.dynamicproxy.Subject,
运行结果为:
before calling public abstract void ***.Subject.request()
From real subject.
after calling public abstract void ***.Subject.request()
从以上代码和结果可以看出,我们并没有显示的调用invoke()方法,但是这个方法确实执行了。下面就整个的过程进行分析一下:
从Client中的代码看,可以从newProxyInstance这个方法作为突破口,我们先来看一下Proxy类中newProxyInstance方法的源代码:
public static Object newProxyInstance(ClassLoader loader,
Class>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
{
if (h == null) {
throw new NullPointerException();
}
/*
* Look up or generate the designated proxy class.
*/
Class cl = getProxyClass(loader, interfaces);
/*
* Invoke its constructor with the designated invocation handler.
*/
try {
/*
* Proxy源码开始有这样的定义:
* private final static Class[] constructorParams = { InvocationHandler.class };
* cons即是形参为InvocationHandler类型的构造方法
*/
Constructor cons = cl.getConstructor(constructorParams);
return (Object) cons.newInstance(new Object[] { h });
} catch (NoSuchMethodException e) {
throw new InternalError(e.toString());
} catch (IllegalAccessException e) {
throw new InternalError(e.toString());
} catch (InstantiationException e) {
throw new InternalError(e.toString());
} catch (InvocationTargetException e) {
throw new InternalError(e.toString());
}
}
class Proxy{
InvocationHandler h=null;
protected Proxy(InvocationHandler h) {
this.h = h;
}
...
}
public final class $Proxy0 extends Proxy implements Subject {
private static Method m1;
private static Method m0;
private static Method m3;
private static Method m2;
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("***.RealSubject").getMethod("request",
new Class[0]);
m2 = Class.forName("java.lang.Object").getMethod("toString",
new Class[0]);
} catch (NoSuchMethodException nosuchmethodexception) {
throw new NoSuchMethodError(nosuchmethodexception.getMessage());
} catch (ClassNotFoundException classnotfoundexception) {
throw new NoClassDefFoundError(classnotfoundexception.getMessage());
}
} //static
public $Proxy0(InvocationHandler invocationhandler) {
super(invocationhandler);
}
@Override
public final boolean equals(Object obj) {
try {
return ((Boolean) super.h.invoke(this, m1, new Object[] { obj })) .booleanValue();
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
@Override
public final int hashCode() {
try {
return ((Integer) super.h.invoke(this, m0, null)).intValue();
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final void request() {
try {
super.h.invoke(this, m3, null);
return;
} catch (Error e) {
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
@Override
public final String toString() {
try {
return (String) super.h.invoke(this, m2, null);
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
}