プロキシモードDynamic Proxies(四、Struts 2.0ブロッカーInterceptor)

4550 ワード

一、概念と注意点:Once you write a dynamic proxy class,you can use it to wrap any object,so
long as the object is an instance of a class that implements an interface that
declares the behavior you want to intercept.動的エージェントは、任意のクラスをラップし、このクラスが実装するインタフェースのいくつかの方法をブロックし、これらの方法を実行するために使用できます.
の前後に適当な動作を加える.    Dynamic proxies in Java let you wrap an object with a proxy that intercepts
the object's calls and that can add behavior before or after passing the call
along.This lets you create reusable behavior that you can drop in on an arbitrary
object, in a fashion similar to aspect-oriented programming.ブロックされた方法の前後に適切な操作を加える--AOPのような「再利用性」を向上させる.    In AOP, an aspect is a combination of aspect——code that you want to drop in
——and point cuts——definitions of execution points in your code where you want
the drop-in code to run.AOPの2つの概念を理解する:1)aspect-再利用可能コード2)point cuts-実行点
 
Samのまとめ:Dynamic Proxiesはブロックであり、いくつかの操作の前後に再利用可能なコードを追加します.記憶する
Javaで動的エージェントを実装するために実装するインタフェースと方法.
 
二、コード
 
動的エージェントの使用java
元のオブジェクトHashSet sがエージェント化されると、HashSetの(任意の)親インタフェースSetに変換され、Setで宣言された任意のメソッドを実行すると、周辺でラップブロック動作^^;
package app.proxy.dynamic;

import java.util.HashSet;
import java.util.Set;

import com.oozinoz.firework.Firecracker;
import com.oozinoz.firework.Sparkler;
import com.oozinoz.utility.Dollars;

/**
 * Show an example of using a dynamic proxy to add behavior to an object. In
 * this example, we add an element of impatience, complaining if any method
 * takes too long to execute.
 */
public class ShowDynamicProxy {
    public static void main(String[] args) {
        Set s = new HashSet();
        s = (Set) ImpatientProxy.newInstance(s); /*   */
        s.add(new Sparkler("Mr. Twinkle", new Dollars(0.05)));
        s.add(new BadApple("Lemon"));
        s.add(new Firecracker("Mr. Boomy", new Dollars(0.25)));

        System.out.println("The set contains " + s.size() + " things.");
    }
}

 
動的エージェントクラスImpatientProxy.java
1)動的エージェントクラスは、InvocationHandlerインタフェースと、そのインタフェース内のinvoke(...)を実装する必要があります.方法;
2)暗記:静的インスタンス化方法newInstance(...)に表示されます.
package app.proxy.dynamic;

import java.lang.reflect.*;

/**
 * This class is an example of a dynamic proxy. Instances of this class wrap a
 * proxied object. This class simply forwards calls to the object it wraps.
 * However, if any method takes a long time to execute, this class will print a
 * warning message.
 */
public class ImpatientProxy implements InvocationHandler {

    private Object obj;

    /**
     * Construct a dynamic proxy around the given object.
     * @param obj the object to wrap
     * @return the proxy
     */
    public static Object newInstance(Object obj) {
        ClassLoader loader = obj.getClass().getClassLoader();	/*  “ ”  */
        Class[] classes = obj.getClass().getInterfaces();		/*  “ ”  */
        return Proxy.newProxyInstance(loader, classes, new ImpatientProxy(obj));	/*  Object  */
        /*
         *  :The returned object will implement all the interfaces that the wrapped
         * object's class implements. We can cast the returned object to any of these 
         * interfaces.
         */
    }

    private ImpatientProxy(Object obj) {
        this.obj = obj;
    }

    /**
     * The method that all dynamic proxies must implement. This dynamic proxy
     * complains when a method takes a long time to return.
     */
    public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
        Object result;
        long t1 = System.currentTimeMillis();
        result = m.invoke(obj, args);
        long t2 = System.currentTimeMillis();
        if (t2 - t1 > 10) {
            System.out.println("> It takes " + (t2 - t1) + " millis to invoke " + m.getName()
                    + "() with");
            for (int i = 0; i < args.length; i++) 
                System.out.println(">     arg[" + i + "]: " + args[i]);
        }
        return result;
    }
}