二、动态代理
Java中动态代理的实现,关键就是这两个东西:Proxy、InvocationHandler,下面从InvocationHandler接口中的invoke方法入手,简单说明一下Java如何实现动态代理的。
首先,invoke方法的完整形式如下:
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
- {
- method.invoke(obj, args);
- return null;
- }
首先猜测一下,method是调用的方法,即需要执行的方法;args是方法的参数;proxy,这个参数是什么?以上invoke()方法的实现即是比较标准的形式,我们看到,这里并没有用到proxy参数。查看JDK文档中Proxy的说明,如下:
Java代码
- 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参数传递的即是代理类的实例。
为了方便说明,这里写一个简单的例子来实现动态代理。
Java代码
- //抽象角色(动态代理只能代理接口)
- public interface Subject {
- public void request();
- }
Java代码
- //真实角色:实现了Subject的request()方法
- public class RealSubject implements Subject{
- public void request(){
- System.out.println("From real subject.");
- }
- }
Java代码
- //实现了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;
- }
- }
Java代码
- //客户端:生成代理实例,并调用了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();
- }
- }
Xml代码
- 运行结果如下:此处省略了包名,***代替
- 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 ab
- 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.