java--02--動態代理

來源:互聯網
上載者:User

標籤:介面   圖片   code   use   載器   open   ==   dao   span   

 1 動態代理, 2     1)代理對象,不需要實現介面; 3     2)代理對象的產生,是利用JDK API, 動態在記憶體中構建代理對象(需要我們指定建立 代理對象/目標對象 實現的介面的類型;); 4     3)  動態代理, JDK代理, 介面代理; 5  6 JDK中組建代理程式對象的API: 7 |-- Proxy 8     static Object newProxyInstance( 9 ClassLoader loader,       指定當前目標對象使用類載入器10  Class<?>[] interfaces,     目標對象實現的介面的類型11 InvocationHandler h       事件處理器12 )  13 14 15 動態代理總結:16     代理對象不需要實現介面,但是目標對象一定要實現介面;否則不能用動態代理!17     (class  $Proxy0  implements IuserDao)18 19 思考:20     有一個目標對象,想要功能擴充,但目標對象沒有實現介面,怎樣功能擴充?21     Class  UserDao{}22     // 子類的方式23     Class subclass  extends  UserDao{}24     25     以子類的方式實現(cglib代理)
View Code

介面, 要乾的活

1 public interface IUserDao {2 3     void save();4     5     void update();6 }
View Code

目標對象必須實現介面

 1 // 目標對象 2 public class UserDao implements IUserDao{ 3  4     @Override 5     public void save() { 6         System.out.println("目標對象,插入資料到DB中..."); 7     } 8  9     @Override10     public void update() {11         System.out.println("目標對象, 修改資料...");12         13     }14 15 }
View Code

代理對象不用實現介面

 1 public class ProxyFactory { 2     private Object target; 3     public ProxyFactory(Object target){ 4         this.target = target; 5     } 6  7     public Object getProxyInstance(){ 8          9         return Proxy.newProxyInstance(10                 target.getClass().getClassLoader(), 11                 target.getClass().getInterfaces(),12                 new InvocationHandler(){13 14                     @Override15                     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {16                         System.out.println("開啟事務");17                         Object value = method.invoke(target, args);//== target....  args是方法的參數18                         System.out.println("提交事務");19                         return value;20                     }21                     22                 }23 24             );25     }26 }
View Code

幹吧...

 1 public class ProxyTest { 2  3     public static void main(String[] args) { 4         //2.動態代理 5         IUserDao target = new UserDao(); 6         IUserDao proxy = (IUserDao) new ProxyFactory(target).getProxyInstance(); 7         proxy.save(); 8         proxy.update(); 9         /*10          開啟事務11         目標對象,插入資料到DB中...12         提交事務13         14         開啟事務15         目標對象, 修改資料...16         提交事務17          */18     }19 }
View Code

 

java--02--動態代理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.