JAVA动态代理

静态代理

在介绍动态代理之前,我们先来看看静态代理的实现过程,代理我们可以理解为为其他对象提供一种代理以控制对这个对象的访问。

首先我们创建一个接口:

public interface Animal {
   public void sleep();
   public void run();
}

接下来,我们创建一个实现类:

public class Dog implements Animal {

   @Override
   public void sleep()
   {
      System.out.println("dog sleep...");
   }

   @Override
   public void run()
   {
      System.out.println("dog run...");
   }
}

我们现在要对Dog类进行代理,所以需要创建代理类:

package dynamicproxy;

public class DogProxy implements Animal {
   private Animal animal = null;

   public DogProxy(Animal animal)
   {
      this.animal = animal;
   }

   @Override
   public void sleep()
   {
      System.out.println("before invoke sleep...");
      animal.sleep();
      System.out.println("after invoke sleep...");
   }

   @Override
   public void run()
   {
      System.out.println("before invoke run...");
      animal.run();
      System.out.println("after invoke run...");
   }
}

下面我们用一个测试类测试一下:

package dynamicproxy;

public class Test {
   public static void main(String[] args) throws Exception
   {
      Animal animal = new DogProxy(new Dog());
      animal.sleep();
      animal.run();
   }
}

运行结果:
before invoke sleep…
dog sleep…
after invoke sleep…
before invoke run…
dog run…
after invoke run…

动态代理

静态代理存在大量的重复代码且需要维护多个代理类,维护成本很大,所以使用动态代理是很有必要的。

动态代理需要用到java.lang.reflect.InvocationHandlerjava.lang.reflect.Proxy
InvocationHandler是代理实例的调用处理程序 实现的接口。Proxy提供用于创建动态代理类和实例的静态方法,它还是由这些方法创建的所有动态代理类的超类。

同样的我们还是以静态代理中的例子来演示动态代理的使用,实现同样的功能,我们可以编写一个类实现InvocationHandler接口:

package dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class AnimalInvocationHandler implements InvocationHandler {
   private Object targetObject = null;

   public Object bind(Object targetObject)
   {
      this.targetObject = targetObject;
      return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),
            targetObject.getClass().getInterfaces(), this);
   }

   @Override
   public Object invoke(Object proxy, Method method, Object[] args)
         throws Throwable
   {
      String name = method.getName();
      System.out.println("before invoke " + name + "...");
      Object result = method.invoke(targetObject, args);
      System.out.println("after invoke " + name + "...");
      return result;
   }
}

最后,我们编写一个测试类测试一下:

public class Test {
   public static void main(String[] args) throws Exception
   {
      AnimalInvocationHandler invocationHandler = new AnimalInvocationHandler();
      Animal animal = (Animal) invocationHandler.bind(new Dog());
      animal.sleep();
      animal.run();
   }
}

运行结果:
before invoke sleep…
dog sleep…
after invoke sleep…
before invoke run…
dog run…
after invoke run…

通过对比动态代理与静态代理的实现方式,我们可以发现动态代理更加简洁,更易于维护。

我们可以将动态代理的相关实现提取出来,形成工具类,方便以后使用:

import java.lang.reflect.Method;

/** * 动态代理支持接口 * * @author jianggujin * */
public interface DynamicProxySupport {
   /** * 方法执行前执行 * * @param backIfFalse * 如果不继续执行的返回数据 * @param delegate * 委托对象 * @param proxy * 代理对象 * @param method * 要执行的委托对象的方法 * @param args * 要执行的委托对象的方法的参数 * @return true 继续向下执行,否则不执行 */
   public boolean beforeInvoke(Object backIfFalse, Object delegate,
         Object proxy, Method method, Object[] args);

   /** * 方法执行后执行 * * @param result * 要执行的委托对象的方法的结果 * @param delegate * 委托对象 * @param proxy * 代理对象 * @param method * 要执行的委托对象的方法 * @param args * 要执行的委托对象的方法的参数 */
   public void afterInvoke(Object result, Object delegate, Object proxy,
         Method method, Object[] args);
}
import java.lang.reflect.Method;

/** * 动态代理支持接口实现类,实现代理指定方法,方法规则为 * {@link #beforeMethodInvoke(Object, Object, Object, Object[])} * ,返回类型为Boolean(boolean)或 * {@link #afterMethodInvoke(Object, Object, Object, Object[])},返回类型为void(不做拦截) * ,分别用于方法前与方法后执行,其中Method为实际需要代理的方法名,且方法首字母大写 * * @author jianggujin * */
public class MethodDynamicProxy implements DynamicProxySupport {
   private final String BEFORE = "before%c%sInvoke";
   private final String AFTER = "after%c%sInvoke";
   private final Class<?> OBJ_ARRAY = new Object[0].getClass();

   /** * 获得需要执行的方法 * * @param format * @param method * @return * @throws NoSuchMethodException */
   private Method getInvokeMethod(String format, Method method)
         throws NoSuchMethodException
   {
      String methodName = method.getName();
      Method invokeMethod = this.getClass()
            .getMethod(
                  String.format(format, Character.toUpperCase(methodName
                        .charAt(0)), methodName.length() == 1 ? "" : method
                        .getName().substring(1)), Object.class, Object.class,
                  Object.class, OBJ_ARRAY);
      return invokeMethod;
   }

   public boolean beforeInvoke(Object backIfFalse, Object delegate,
         Object proxy, Method method, Object[] args)
   {
      try
      {
         Method before = getInvokeMethod(BEFORE, method);
         if (!before.getReturnType().getSimpleName()
               .equalsIgnoreCase("boolean"))
         {
            throw new Exception("the method " + before.getName()
                  + "return type is not Boolean");
         }
         return (Boolean) before.invoke(this, backIfFalse, delegate, proxy,
               args);
      }
      catch (NoSuchMethodException e)
      {
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }
      return true;
   }

   public void afterInvoke(Object result, Object delegate, Object proxy,
         Method method, Object[] args)
   {
      try
      {
         Method after = getInvokeMethod(AFTER, method);
         after.invoke(this, result, delegate, proxy, args);
      }
      catch (NoSuchMethodException e)
      {
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }
   }
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/** * 动态代理,获得委托对象的代理对象 * * @author jianggujin * * @param <P> * 接口 * @param <C> * 实现类 */
public class DynamicProxy<P, C> implements InvocationHandler {
   /** 委托对象 **/
   private Object delegate;
   /** 动态代理支持接口 **/
   private DynamicProxySupport dynamicProxySupport = null;

   /** * 返回指定类的代理对象,通过默认构造方法创建委托对象 * * @param clazz * 委托对象类 * @return <P> * p * @throws InstantiationException * @throws IllegalAccessException */
   @SuppressWarnings("unchecked")
   public P bind(Class<C> clazz) throws InstantiationException,
         IllegalAccessException
   {
      this.delegate = clazz.newInstance();
      // 取得代理对象
      return (P) Proxy.newProxyInstance(clazz.getClassLoader(),
            clazz.getInterfaces(), this);
   }

   /** * 返回指定类的代理对象,通过默认构造方法创建委托对象 * * @param clazz * 委托对象类 * @return <P> * p * @throws InstantiationException * @throws IllegalAccessException */
   @SuppressWarnings("unchecked")
   public P bind(Class<C> clazz, DynamicProxySupport dynamicProxySupport)
         throws InstantiationException, IllegalAccessException
   {
      this.delegate = clazz.newInstance();
      this.dynamicProxySupport = dynamicProxySupport;
      // 取得代理对象
      return (P) Proxy.newProxyInstance(clazz.getClassLoader(),
            clazz.getInterfaces(), this);
   }

   /** * 绑定委托对象并返回一个代理对象 * * @param target * @return <P> * p */
   @SuppressWarnings("unchecked")
   public P bind(C target)
   {
      this.delegate = target;
      // 取得代理对象
      return (P) Proxy.newProxyInstance(target.getClass().getClassLoader(),
            target.getClass().getInterfaces(), this);
   }

   /** * 绑定委托对象并返回一个代理对象 * * @param target * @return <P> * p */
   @SuppressWarnings("unchecked")
   public P bind(C target, DynamicProxySupport dynamicProxySupport)
   {
      this.delegate = target;
      this.dynamicProxySupport = dynamicProxySupport;
      // 取得代理对象
      return (P) Proxy.newProxyInstance(target.getClass().getClassLoader(),
            target.getClass().getInterfaces(), this);
   }

   /** * 执行方法 * * @param proxy * 代理对象 * @param method * 要执行的委托对象的方法 * @param args * 要执行的委托对象的方法的参数 */
   public Object invoke(Object proxy, Method method, Object[] args)
         throws Throwable
   {
      Object result = null;
      if (dynamicProxySupport != null)
      {
         Object obj = new Object();
         if (dynamicProxySupport.beforeInvoke(obj, delegate, proxy, method,
               args))
         {
            result = method.invoke(delegate, args);
            dynamicProxySupport.afterInvoke(result, delegate, proxy, method,
                  args);
         }
         else
         {
            return obj;
         }
      }
      else
      {
         result = method.invoke(delegate, args);
      }
      return result;
   }

   /** * 获得动态代理支持接口 * * @return dynamicProxySupport */
   public DynamicProxySupport getDynamicProxySupport()
   {
      return dynamicProxySupport;
   }

   /** * 设置动态代理支持接口 * * @param dynamicProxySupport */
   public void setDynamicProxySupport(DynamicProxySupport dynamicProxySupport)
   {
      this.dynamicProxySupport = dynamicProxySupport;
   }

   /** * 获得委托对象 * * @return <C> c */
   @SuppressWarnings("unchecked")
   public C getDelegate()
   {
      return (C) delegate;
   }
}

我们可以这样使用工具类

import java.lang.reflect.Method;

public class Test {
   public static void main(String[] args) throws Exception
   {
      DynamicProxy<Animal, Dog> dynamicProxy = new DynamicProxy<Animal, Dog>();
      Animal animal = dynamicProxy.bind(Dog.class, new DynamicProxySupport()
      {

         @Override
         public boolean beforeInvoke(Object backIfFalse, Object delegate,
               Object proxy, Method method, Object[] args)
         {
            System.out.println("before invoke " + method.getName());
            return true;
         }

         @Override
         public void afterInvoke(Object result, Object delegate, Object proxy,
               Method method, Object[] args)
         {
            System.out.println("after invoke " + method.getName());
         }
      });
      animal.sleep();
      animal.run();
   }
}

运行结果:
before invoke sleep
dog sleep…
after invoke sleep
before invoke run
dog run…
after invoke run

如果我们想代理指定的方法,我们可以这样去写:

package dynamicproxy;

public class Test {
   public static void main(String[] args) throws Exception
   {
      DynamicProxy<Animal, Dog> dynamicProxy = new DynamicProxy<Animal, Dog>();
      Animal animal = dynamicProxy.bind(Dog.class, new MethodDynamicProxy()
      {
         public boolean beforeSleepInvoke(Object backIfFalse, Object delegate,
               Object proxy, Object[] args)
         {
            System.out.println("before invoke sleep");
            return false;
         }

         public boolean beforeRunInvoke(Object backIfFalse, Object delegate,
               Object proxy, Object[] args)
         {
            System.out.println("before invoke run");
            return true;
         }

         public void afterSleepInvoke(Object backIfFalse, Object delegate,
               Object proxy, Object[] args)
         {
            System.out.println("after invoke sleep");
         }

         public void afterRunInvoke(Object backIfFalse, Object delegate,
               Object proxy, Object[] args)
         {
            System.out.println("after invoke run");
         }
      });
      animal.sleep();
      animal.run();
   }
}

运行结果:
before invoke sleep
before invoke run
dog run…
after invoke run

你可能感兴趣的:(java,动态代理,代理,静态代理)