Person p = new Person("fyq", 28);
Thing person = (Thing)Proxy.newProxyInstance(Thing.class.getClassLoader(), new Class[]{Thing.class}, new ThingInvocationHandler(p));
参数一:加载代理类的加载器
参数二:代理类需要实现的接口
参数三:初始化代理类实现代理
动态代理主要就是调用这个方法,这个方法完成三步:
1)通过参数二动态生成一个代理类(这个类要继承Proxy,实现参数二传入的接口)
2)通过参数一动态加载代理类
3)通过参数三初始化代理类对象。
1、动态代理类的生成必须要有其需要实现的业务接口,下面看实现Thing接口的代理类,依赖参数二生成动态代理类。
业务接口:
package proxy;
public interface Thing {
public void introduce();
public void introduce1(String sex);
}
实现Thing接口的动态代理类:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
import proxy.Thing;
public final class Proxy0 extends Proxy
implements Thing
{
private static Method m3;
private static Method m1;
private static Method m4;
private static Method m0;
private static Method m2;
public Proxy0()
throws
{
super(paramInvocationHandler);
}
public final void introduce()
throws
{
try
{
this.h.invoke(this, m3, null);
return;
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
public final boolean equals()
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 void introduce1()
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 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 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
{
m3 = Class.forName("proxy.Thing").getMethod("introduce", new Class[0]);
m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
m4 = Class.forName("proxy.Thing").getMethod("introduce1", new Class[] { Class.forName("java.lang.String") });
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
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());
}
}
}
通过classLoader动态加载代理类到内存
3、初始化代理类
代理类继承自Proxy,InvocationHandler是Proxy的成员变量,代理类实现的业务接口中调用InvocationHandler的invoke方法。因此我们需要实现InvocationHandler接口的invoke方法来实现自己的代理。
package proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class ThingInvocationHandler implements InvocationHandler{
Thing t;
public ThingInvocationHandler(Thing t)
{
this.t = t;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("before invoke!");
Object result = method.invoke(t, args);
System.out.println("after invoke!");
return result;
}
}
初始化ThingInvocationHandler对象时必须传入实现了Thing接口的对象,在invoke方法中要调用该对象的方法。
Thing接口业务实现:
package proxy;
public class Person implements Thing{
String name;
int age;
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
public void introduce()
{
System.out.println("My name is " + name + ", i am " + age + " years old.");
}
public void introduce1(String sex)
{
System.out.println("My name is " + name + ", i am " + age + " years old." + "sex:" + sex);
}
}
4、通过动态代理对象调用接口方法
person.introduce();
person.introduce1("女");
结果输出:
before invoke!
My name is fyq, i am 28 years old.
after invoke!
before invoke!
My name is fyq, i am 28 years old.sex:女
after invoke!