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

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

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

    posts - 165, comments - 198, trackbacks - 0, articles - 1
      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    CGLib 學(xué)習(xí)

    Posted on 2007-11-05 10:22 G_G 閱讀(5786) 評(píng)論(3)  編輯  收藏 所屬分類: AOP
    看hbn 源代碼 發(fā)現(xiàn)用了 CGlib 這就看看這個(gè)jar 。特留個(gè)文 ^_^(轉(zhuǎn))http://www.nirvanastudio.org/java/cglib-%E6%8C%87%E5%8D%97.html

    CGlib 就2個(gè)例能說(shuō)明一切?
    先是使用類? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??
    public?class?MyClass?{
    ?
    ????
    public?void?method()?{
    ????????System.out.println(
    "MyClass.method()");
    ????}

    ????
    public?void?method2()?{
    ????????System.out.println(
    "MyClass.method2()");
    ????}
    }

    例1:
    import?java.lang.reflect.Method;
    ?
    import?net.sf.cglib.proxy.Enhancer;
    import?net.sf.cglib.proxy.MethodProxy;
    import?net.sf.cglib.proxy.MethodInterceptor;
    ?
    public?class?Main?{
    ?
    ????
    public?static?void?main(String[]?args)?{
    ?
    ????????Enhancer?enhancer?
    =?new?Enhancer();
    ??? ??
    ??? ??? //在這代理了
    ????????enhancer.setSuperclass(MyClass.
    class);
    ????????enhancer.setCallback(?
    new?MethodInterceptorImpl()?);
    ?
    ??????? // 創(chuàng)造 代理 (動(dòng)態(tài)擴(kuò)展了MyClass類)
    ????????MyClass?my?
    =?(MyClass)enhancer.create();
    ?
    ????????my.method();
    ????}
    ?
    ????
    private?static?class?MethodInterceptorImpl?implements?MethodInterceptor?{
    ????????
    ????????
    public?Object?intercept(Object?obj,?
    ????????????????????????????????Method?method,?
    ????????????????????????????????Object[]?args,?
    ????????????????????????????????MethodProxy?proxy)?
    throws?Throwable?{
    ?
    ????????????System.out.println(method);
    ?
    ????????????proxy.invokeSuper(obj,?args);
    ?
    ????????????
    return?null;
    ????????}
    ????}
    }


    例2:
    import?java.lang.reflect.Method;


    import?net.sf.cglib.proxy.Enhancer;
    import?net.sf.cglib.proxy.MethodProxy;
    import?net.sf.cglib.proxy.MethodInterceptor;
    import?net.sf.cglib.proxy.NoOp;
    import?net.sf.cglib.proxy.Callback;
    import?net.sf.cglib.proxy.CallbackFilter;
    ?
    ?
    public?class?Main2?{
    ?
    ????
    public?static?void?main(String[]?args)?{
    ?
    ????????Callback[]?callbacks?
    =
    ????????????
    new?Callback[]?{?new?MethodInterceptorImpl(),??NoOp.INSTANCE?};
    ?
    ????????Enhancer?enhancer?
    =?new?Enhancer();
    ?
    ????????enhancer.setSuperclass(MyClass.
    class);
    ????????enhancer.setCallbacks(?callbacks?);
    ??????? //添加 方法過(guò)濾器? 返回1為不運(yùn)行 2 為運(yùn)行
    ????????enhancer.setCallbackFilter(?
    new?CallbackFilterImpl()?);
    ?
    ?
    ????????MyClass?my?
    =?(MyClass)enhancer.create();
    ?
    ????????my.method();
    ????????my.method2();
    ????}
    ?
    ????
    private?static?class?CallbackFilterImpl?implements?CallbackFilter?{
    ?
    ????????
    public?int?accept(Method?method)?{
    ?
    ????????????
    if?(?method.getName().equals("method2")?)?{
    ????????????????
    return?1;
    ?
    ????????????}?
    else?{
    ????????????????
    return?0;
    ????????????}
    ????????}
    ????}
    ?
    ????
    private?static?class?MethodInterceptorImpl?implements?MethodInterceptor?{
    ????????
    ????????
    public?Object?intercept(Object?obj,?
    ????????????????????????????????Method?method,?
    ????????????????????????????????Object[]?args,?
    ????????????????????????????????MethodProxy?proxy)?
    throws?Throwable?{
    ?
    ????????????System.out.println(method);
    ?
    ????????????
    return?proxy.invokeSuper(obj,?args);
    ????????}
    ????}
    }


    簡(jiǎn)單不 哈哈 比jdk 中的動(dòng)態(tài)代理好用 ,那還要接口? 不太方便 。

    評(píng)論

    # re: CGLib 學(xué)習(xí)  回復(fù)  更多評(píng)論   

    2007-11-07 10:03 by 雨奏
    @G_G
    兄弟能否說(shuō)說(shuō)用CGLIB有哪些限制或副作用?

    # re: CGLib 學(xué)習(xí)  回復(fù)  更多評(píng)論   

    2007-11-09 17:11 by G_G
    @雨奏
    限制或副作用到?jīng)]感覺(jué)到
    但功能明顯太單薄了
    和aspectj沒(méi)的比 切入表達(dá)式 的aspectj 和只可以硬編碼的切CGlib
    的功能用下就知道 CGlib 太簡(jiǎn)單了
    但他好用,好上手,給5分鐘就上了

    aspectj 我花了2星期才到理解程度 呵呵

    # re: CGLib 學(xué)習(xí)  回復(fù)  更多評(píng)論   

    2009-01-16 17:07 by Hill
    請(qǐng)問(wèn)一下。你真的可以運(yùn)行嗎?我前幾天做了一個(gè)和你這個(gè)差不多的。總報(bào)錯(cuò)。請(qǐng)指點(diǎn)一下啊。
    net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:237)
    at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
    at spring.AOPInstrument.getInstrumentedClass(AOPInstrument.java:28)
    at spring.CGLibTest.main(CGLibTest.java:24)
    Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:324)
    at net.sf.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:384)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:219)
    ... 4 more
    Caused by: java.lang.SecurityException: class "spring.MessageWriter$$EnhancerByCGLIB$$29bc061d"'s signer information does not match signer information of other classes in the same package
    at java.lang.ClassLoader.checkCerts(ClassLoader.java:611)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:532)
    ... 10 more
    Exception in thread "main"

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国产精品亚洲专区在线播放| 四虎永久免费地址在线观看| 一区二区三区免费视频播放器| 亚洲一卡2卡4卡5卡6卡残暴在线| 亚洲尤码不卡AV麻豆| 男女啪啪永久免费观看网站| 131美女爱做免费毛片| 你懂的在线免费观看| 污网站在线观看免费| 亚洲熟妇丰满xxxxx| 亚洲区视频在线观看| 青青草原精品国产亚洲av| 亚洲欧洲成人精品香蕉网| 亚洲国产成人乱码精品女人久久久不卡 | 亚洲第一页在线播放| 亚洲国产另类久久久精品黑人| 亚洲成av人在片观看| 在线观看免费精品国产| 成人性生交视频免费观看| 波多野结衣免费在线| 亚洲综合免费视频| 一级毛片免费播放| 5555在线播放免费播放| 特级精品毛片免费观看| 久久99精品免费视频| 久久久久久夜精品精品免费啦 | 亚洲综合色在线观看亚洲| 四虎影视永久免费视频观看| 精品免费国产一区二区三区| 免费涩涩在线视频网| 永久免费观看的毛片的网站| 无码高潮少妇毛多水多水免费| 91香蕉视频免费| 国产免费久久精品99re丫y| 免费看韩国黄a片在线观看| 国产在线a免费观看| 四虎www成人影院免费观看| 日本一道在线日本一道高清不卡免费| 黄色片在线免费观看| 成年女人毛片免费观看97| 日韩一区二区在线免费观看 |