Java 动态代理实践AOP
生活随笔
收集整理的這篇文章主要介紹了
Java 动态代理实践AOP
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
大家都知道Spring中AOP是通過Java動態代理實現的,今天就來簡單學習下demo。
Java動態代理主要有兩個核心類,InvocationHandler和Proxy。
/*** {@code InvocationHandler} is the interface implemented by* the <i>invocation handler</i> of a proxy instance.** <p>Each proxy instance has an associated invocation handler.* When a method is invoked on a proxy instance, the method* invocation is encoded and dispatched to the {@code invoke}* method of its invocation handler.** @author Peter Jones* @see Proxy* @since 1.3*/ public interface InvocationHandler所有的Handler類要實現InvocationHandler接口,并關聯到Proxy實例上,最后會分發到InvocationHandler的invoke方法上。
/*** {@code Proxy} provides static methods for creating dynamic proxy* classes and instances, and it is also the superclass of all* dynamic proxy classes created by those methods.** <p>To create a proxy for some interface {@code Foo}:* <pre>* InvocationHandler handler = new MyInvocationHandler(...);* Class proxyClass = Proxy.getProxyClass(* Foo.class.getClassLoader(), new Class[] { Foo.class });* Foo f = (Foo) proxyClass.* getConstructor(new Class[] { InvocationHandler.class }).* newInstance(new Object[] { handler });* </pre>* or more simply:* <pre>* Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),* new Class[] { Foo.class },* handler);* </pre>******************************/ public class Proxy implements java.io.Serializable通過該類的靜態方法創建要動態代理的類。
下面看下demo
1. 先創建一個接口
public interface TargetInterface {int targetMethod(int num); }2. 實例化該接口
public class TargetClass implements TargetInterface {@Overridepublic int targetMethod(int number) {System.out.println("調用目標類的方法targetMethod..."); return number; } }3. 創建代理處理類,InvocationHandler子類
public class ProxyHandler implements InvocationHandler {Object concreteClass;public ProxyHandler(Object concreteClass) {this.concreteClass = concreteClass;}@Overridepublic Object invoke(Object proxy, Method method, Object[] args)throws Throwable {System.out.println("proxy:"+proxy.getClass().getName()); System.out.println("method:"+method.getName()); System.out.println("args:"+args[0].getClass().getName()); System.out.println("Before invoke method..."); Object object = method.invoke(concreteClass, args);System.out.println("After invoke method..."); return object; } } proxy: 指代我們所代理的那個真實對象 method: 指代的是我們所要調用真實對象的某個方法的Method對象 args: 指代的是調用真實對象某個方法時接受的參數 public class Example {public static void main(String[] args) {TargetClass cc = new TargetClass();InvocationHandler ih = new ProxyHandler(cc);TargetInterface tf = (TargetInterface) Proxy.newProxyInstance(cc.getClass().getClassLoader(), cc.getClass().getInterfaces(), ih);int i = tf.targetMethod(5);} } public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentExceptionloader: 一個ClassLoader對象,定義了由哪個ClassLoader對象來對生成的代理對象進行加載 interfaces: 一個Interface對象的數組,表示的是我將要給我需要代理的對象提供一組什么接口,如果我提供了一組接口給它,那么這個代理對象就宣稱實現了該接口(多態),這樣我就能調用這組接口中的方法了 h: 一個InvocationHandler對象,表示的是當我這個動態代理對象在調用方法的時候,會關聯到哪一個InvocationHandler對象上注意:通過?Proxy.newProxyInstance 創建的代理對象是在jvm運行時動態生成的一個對象,它并不是我們的InvocationHandler類型,也不是我們定義的那組接口的類型,而是在運行是動態生成的一個對象,并且命名方式都是這樣的形式,以$開頭,proxy為中,最后一個數字表示對象的標號。
動態代理有個缺陷,就是創建時需要參數interfaces,即被代理的類,需要實現該接口。
轉載于:https://my.oschina.net/android520/blog/700945
總結
以上是生活随笔為你收集整理的Java 动态代理实践AOP的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android中WebView加载本地H
- 下一篇: jQuery Lightbox图片放大预