java動態代理(知識點掃盲)

來源:互聯網
上載者:User

標籤:動態代理   spring動態代理   

動態代理就先說代理模式,然後是動態代理

代理模式中,會有代理對象,和被代理對象(目標業務對象)。代理對象會攔截對目標業務對象的訪問。類似於現實中的老師和經紀人。

/** 老師的介面*/public interface drawer {public void sing(double money);public void draw(double money);public void sleep();}
/** 一個具體的老師:徐老師*/public class Xulaoshi implements drawer {@Overridepublic void sing(double money) {System.out.println("徐老師教了一首歌!");System.out.println("徐老師賺了" + money + "RMB");}@Overridepublic void draw(double money) {System.out.println("徐老師教了一節畫畫!");System.out.println("徐老師賺了" + money + "RMB");}@Overridepublic void sleep() {System.out.println("徐老師太累了。。。休息一會兒");}}/** 代理類A,代理徐老師*/public class A implements drawer {// 持有真實業務對象private drawer s;// 接收真實業務對象public A(drawer s) {this.s = s;}@Overridepublic void sing(double money) {// 做一個攔截if (money < 100000) {System.out.println("錢太少,不去");return;}System.out.println("A抽取" + money * 0.6 + "提成");s.sing(money * 0.4);}@Overridepublic void draw(double money) {// 做一個攔截if (money < 500000) {System.out.println("錢太少,加錢!");return;}System.out.println("A抽取" + money * 0.6 + "提成");s.draw(money * 0.4);}@Overridepublic void sleep() {System.out.println("幫忙定個酒店。。。");s.sleep();}}
/** 示範:代理模式* 代理:代理對象主要用來攔截目標業務對象(被代理對象)的訪問。*  A:代理對象要持有真實業務對象。(通過建構函式接收業務對象,並用成員變數記錄)*  B:代理對象要和真實業務對象,具備相同的行為方法(實現相同介面)*  C:代理對象攔截對真實對象的訪問,可以修改訪問的參數、傳回值,甚至攔截訪問* 代理模式與裝飾模式區別*  A:代理模式側重與攔截,裝飾模式側重與加強*  B:Java中內建的代理中,只能對介面進行代理。裝飾只需要與被裝飾類有共同父類。*/public class ProxyDemo01 {public static void main(String[] args) {// 建立被代理對象Xulaoshi Xulaoshi = new Xulaoshi();// 建立代理對象A  a= new A(Xulaoshi);// 存取方法a.sing(500);System.out.println("----------");a.draw(1000000);System.out.println("----------");a.sleep();}}

下面動態代理

/** 示範:動態代理* 動態代理:在程式運行過程中產生的一個代理對象* 在程式啟動並執行過程中,動態在記憶體中產生一個類,代理需要被代理的目標對象,並且產生這個代理類的對象。* Java如何憑空產生一個代理類,並且代理徐老師的呢?我們要查看一個API:*  static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)*   ClassLoader loader :類載入器,一般用被代理對象的類載入器*   Class[] interfaces : 被代理對象的介面的Class對象數組,Class<?>[] getInterfaces()  *   InvocationHandler h : 調用處理器*/public class ProxyDemo02 {public static void main(String[] args) {// 建立被代理對象final Xulaoshi Xulaoshi = new Xulaoshi();// 先準備動態代理的三大參數// 被代理類的類載入器ClassLoader loader =Xulaoshi .getClass().getClassLoader();// 被代理對象的介面的Class對象數組Class<?>[] interfaces =Xulaoshi .getClass().getInterfaces();//Xulaoshi .getClass().getInterfaces();{drawer.class}// 調用處理器InvocationHandler h = new InvocationHandler(){// 當我們調用代理對象的功能時,代理對象,其實都來調用了處理器中的invoke功能// 因此,我們需要在invoke中寫攔截的邏輯!public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {// Object proxy : 是代理對象本身// Method method :當前正在被調用的真實方法的對象// Object[] args : 當前正在被調用的真實方法的真實參數String name = method.getName();// 判斷方法的名稱,做出不同的攔截邏輯if( "sing".equals(name) || "draw".equals(name)){double money = (double) args[0];double min = "sing".equals(name) ? 100000 : 500000;if (money < min) {System.out.println("錢太少,不去");return null;}System.out.println("A抽取" + money * 0.6 + "提成");return method.invoke(Xulaoshi, money * 0.4);}return method.invoke(Xulaoshi, args);}};// static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)drawer p = (drawer) Proxy.newProxyInstance(loader, interfaces, h);p.sing(100);p.draw(1000000);p.sleep();}}

java動態代理(知識點掃盲)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.