<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    動態代理,作為實現AOP的方式之一,已經得到廣泛的應用.本人看了很多書關于動態代理的介紹,基本就是不知所云. 所以最終自己做了一個例子,才感到有點明白,下面是我的代碼

    (注: 說明同一個包里面的類的類加載器(ClassLoader)是一樣的)

    package ?pear;?

    ??

    // import?org.springframework.aop.Advisor;?

    // import?org.springframework.aop.BeforeAdvice;?

    import ?java.lang.reflect. * ;?

    ??

    interface ?SayInterface {?????????? // ?被代理接口?

    ???????
    public ? void ?say();?

    ???????
    public ? void ?saytwo();?

    }
    ?

    class ?Say? implements ?SayInterface {???? // ?被代理的類?

    ???????
    public ? void ?say() {?

    ??????????????System.out.println(
    " ?in?say? " );?

    ???????}
    ?

    ???????
    public ? void ?saytwo() {?

    ??????????????System.out.println(
    " in?say?2 " );?

    ???????}
    ?

    }
    ?

    ??

    class ?SayHandler? implements ?InvocationHandler {????? // ?

    ???????
    private ?Say?mysay? = ? new ?Say();????????? // 先生成一個"被代理類"的對象?

    ???????
    public ?Object?invoke(Object?proxy,Method?method,Object[]?args) throws ?Exception {?

    ??????????????System.out.println(
    " Before " );???? // 方法調用之前輸出Before?

    ???????????method.invoke(mysay,args);?????????
    // 原來方法在這里才真正被調用?

    ??????????????
    // method.invoke(saytwo,args);?

    ??????????????System.out.println(
    " After " );?????? // 方法調用之后輸出After?

    ??????????????
    return ? null ;?

    ???????}
    ?

    }
    ?

    ??

    public ? class ?Main? {?

    ?
    /**
    ??*?
    @param ?args
    ??
    */

    ?
    public ? static ? void ?main(String[]?args)? {
    ??
    // ?TODO?Auto-generated?method?stub
    ??SayHandler?handler? = ? new ?SayHandler();
    ??SayInterface?si?
    = ?(SayInterface)Proxy.newProxyInstance(
    ????SayInterface.
    class .getClassLoader(), new ?Class[] {SayInterface. class } ,handler);
    /** ??SayInterface?si?=?(SayInterface)Proxy.newProxyInstance(
    //???????handler.getClass().getClassLoader(),handler.say.getClass().getInterfaces(),handler);
    ??//被代理接口的類加載器,被加載的類,和。。。。
    ????????SayInterface?si?=?(SayInterface)Proxy.newProxyInstance(handler.getClass().getClassLoader(),);???
    */
    ?
    ???si.say();
    ??si.saytwo();
    ??System.out.println(
    " the?end. " );
    ?}
    ?

    }


    posted on 2006-06-10 20:46 pear 閱讀(1185) 評論(2)  編輯  收藏
    Comments

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
     
    主站蜘蛛池模板: 免费A级毛片av无码| 国产精品酒店视频免费看| 亚洲精品第一国产综合野| 日韩成人免费aa在线看| 中文在线免费观看| 久久精品国产亚洲av麻豆图片 | 三年片免费高清版| 亚洲国产午夜电影在线入口| 亚洲国产人成精品| 51在线视频免费观看视频| 黄色一级免费网站| 亚洲黄色在线电影| 亚洲国产综合精品中文字幕| 亚洲一区二区三区免费观看| 一级黄色片免费观看| 亚洲成a人片在线看| 亚洲国产精品成人精品无码区| 成人午夜性A级毛片免费| 久久免费视频网站| 国产AV无码专区亚洲AV蜜芽 | 亚洲午夜视频在线观看| 免费在线观看一级毛片| 91视频免费网址| 杨幂最新免费特级毛片| 亚洲国产人成在线观看| 亚洲综合日韩中文字幕v在线| 亚洲人成电影在线播放| 午夜影视在线免费观看| **一级毛片免费完整视| 一区二区三区免费视频网站| 亚洲精品无码人妻无码| 亚洲黄色片在线观看| 久久亚洲色一区二区三区| 永久在线毛片免费观看| 日本免费网址大全在线观看| 高清一区二区三区免费视频| 国产免费区在线观看十分钟| 精品在线免费视频| 亚洲s码欧洲m码吹潮| 亚洲综合无码一区二区痴汉| 亚洲国产精品日韩在线观看|