2008年9月16日星期二

Spring代理 之 java代理

  1. package home.dong.aop;

  2. import home.dong.impl.Lion;
  3. import home.dong.impl.Tiger;
  4. import home.dong.inter.Animal;
  5. import java.lang.reflect.InvocationHandler;
  6. import java.lang.reflect.Method;
  7. import java.lang.reflect.Proxy;
  8. import org.springframework.aop.framework.ProxyFactory;
  9. public class J2EE_Base_AOP {
  10.     /**
  11.      * java代理,要求代理目标必须时间某个接口,则在通过代理得到对象实例时必须将得到的对象转换成被代理对象的借口类型,否则会转型错误抛出
  12.      */

  13.     /**
  14.      * java代理 I 匿名内部类实现, 直接实现InvocationHandler接口
  15.      * @param tiger
  16.      */
  17.     public void proxy_A_Animal_1(final Tiger tiger) {
  18.         Animal animal = (Animal) Proxy.newProxyInstance(tiger.getClass()
  19.                 .getClassLoader(), tiger.getClass().getInterfaces(),
  20.                 new InvocationHandler() {
  21.                     public Object invoke(Object proxy, Method method,
  22.                             Object[] args) throws Throwable {
  23.                         System.out.println("我在这里代理了....... 下面要执行的方法是:  "
  24.                                 + method.getName());
  25.                         if (method.getName().equals("_Fight")) {
  26.                             System.out.println("fighting!!!!!!!!!!!!!!!!");
  27.                             System.out.println("对手是====" + args[0]);
  28.                         }
  29.                         return method.invoke(tiger, args);
  30.                     }
  31.                 });
  32.         System.out.println(animal._Drink("water"));
  33.         System.out.println(animal._Eat("meat"));
  34.         System.out.println(animal._Fight("Lion"));
  35.     }

  36.     /**
  37.      * java代理 II
  38.      */
  39.     public void proxy_A_Animal_2() {
  40.         // 实例化要被代理的类
  41.         Lion lion = new Lion();
  42.         Animal animal = (Animal) Proxy.newProxyInstance(Thread.currentThread()
  43.                 .getContextClassLoader(), lion.getClass().getInterfaces(),
  44.                 new MyInvocationHandler<Lion>(lion));
  45.         animal._Drink("Milk");
  46.         animal._Eat("Meat");
  47.         animal._Fight("Tiger");
  48.     }

  49.     // 内部类 实现InvocationHandler接口 对方法进行拦截
  50.     public class MyInvocationHandler<T> implements InvocationHandler {
  51.         private T targetClass;
  52.         MyInvocationHandler(T target) {
  53.             this.targetClass = target;
  54.         }

  55.         /**
  56.          * 调用代理目标的方法
  57.          * 
  58.          * @param proxy
  59.          *            代理实例
  60.          * @param mthod
  61.          *            代理目标的方法
  62.          * @param args
  63.          *            代理目标的方法参数
  64.          */
  65.         public Object invoke(Object proxy, Method method, Object[] args)
  66.                 throws Throwable {
  67.             System.out.println("这里进行真正的方法拦截--------拦截将要执行的方法");
  68.             System.out.println("=====================================");
  69.             System.out.println("要执行的方法是: "
  70.                     + method.getDeclaringClass().getName() + " 的 "
  71.                     + method.getName());
  72.             /*
  73.              * 注意:method.invoke(Object obj,Object... args)
  74.              * 方法中第一个参数不能传递proxy,否则会出现无限递归调用,应该传递实际的代理目标的实例
  75.              */
  76.             Object obj = method.invoke(targetClass, args);
  77.             System.out.println(obj.toString());
  78.             return obj;
  79.         }
  80.     }

  81.     public static void main(String[] args) {
  82.         J2EE_Base_AOP aop = new J2EE_Base_AOP();
  83.         aop.proxy_A_Animal_1(new Tiger());
  84.         System.out.println("\n\n\n");
  85.         aop.proxy_A_Animal_2();
  86.     }

  87. }

标签:

1 条评论:

Blogger trustno1 说...

java编程示例代码
java programming example读取URL的内容

2019年2月5日 12:57  

发表评论

订阅 博文评论 [Atom]

<< 主页