1.什麼是動態代理?
2.為什麼使用動態代理?
3.使用它有哪些好處?
4.哪些地方需要動態代理?
--------------------分隔線-----------------------------
和動態代理有關的有兩個類
1.interface InvocationHandler
Object invoke(Object proxy, Method method, Object[] args)
只這一個方法,後面再說
2.class Proxy
真正表示動態代理的類,提供兩個靜態方法:
Class<?> getProxyClass(ClassLoader loader, Class<?>[] interface)
用來產生代理類,參數要提供interface數組,它會產生這些interface的“虛擬實現”,
用來冒充真實的對象。
Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
產生代理對象,多了InvocationHandler參數(只是InvocationHandler介面的實作類別),
它與代理對象關聯,當請求分發到代理對象後,會自動執行h.invoke(...)方法,
invoke方法就是我們用來做N多事情的地方 -_-。
--------------------分隔線-----------------------------
看完上面的代碼,大致明白動態代理的含義:
A介面有c方法,類B實現A介面,原本應該是執行B類中的c方法,可現在不這樣做;
我聲明產生B類的代理類B',由它來冒充B類的“兄弟”並“實現”A介面,
對外界來說B'應該也有c方法,可當真正調用它的時候,
它會去執行與它關聯InvocationHandler的invoke()方法,
在這個方法裡面你可以做很多事情。這樣,這個請求就被“代理”到其它地方去了。
下面是根據我的理解畫的一個說明圖
--------------------分隔線-----------------------------
引用網上的一個例子來說明問題(有部分改動,轉載自:http://callan.iteye.com/blog/161806)
真實的介面:
public interface Hello { void sayHello(String to); void print(String p); }
它的真實實作類別:
public class HelloImpl implements Hello { public void sayHello(String to) { System.out.println("Say hello to " + to); } public void print(String s) { System.out.println("print : " + s); } }
在這裡產生與代理類相關聯的InvocationHandler對象
public class LogHandler implements InvocationHandler { private Object dele; public LogHandler(Object obj) { this.dele = obj; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { doBefore(); //在這裡完全可以把下面這句注釋掉,而做一些其它的事情 Object result = method.invoke(dele, args); after(); return result; } private void doBefore() { System.out.println("before...."); } private void after() { System.out.println("after...."); }}
最後是測試類別:
public class ProxyTest { public static void main(String[] args) { HelloImpl impl = new HelloImpl(); LogHandler handler = new LogHandler(impl); //這裡把handler與impl新產生的代理類相關聯 Hello hello = (Hello) Proxy.newProxyInstance(impl.getClass().getClassLoader(), impl.getClass().getInterfaces(), handler); //這裡無論訪問哪個方法,都是會把請求轉寄到handler.invoke hello.print("All the test"); hello.sayHello("Denny"); }}
這裡是輸出結果:
before....print : All the testafter....before....Say hello to Dennyafter....