日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 动态代理实践AOP

發布時間:2025/1/21 java 101 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。