<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 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    CGLib 學習

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

    CGlib 就2個例能說明一切?
    先是使用類? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??
    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()?);
    ?
    ??????? // 創造 代理 (動態擴展了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?);
    ??????? //添加 方法過濾器? 返回1為不運行 2 為運行
    ????????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);
    ????????}
    ????}
    }


    簡單不 哈哈 比jdk 中的動態代理好用 ,那還要接口? 不太方便 。

    評論

    # re: CGLib 學習  回復  更多評論   

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

    # re: CGLib 學習  回復  更多評論   

    2007-11-09 17:11 by G_G
    @雨奏
    限制或副作用到沒感覺到
    但功能明顯太單薄了
    和aspectj沒的比 切入表達式 的aspectj 和只可以硬編碼的切CGlib
    的功能用下就知道 CGlib 太簡單了
    但他好用,好上手,給5分鐘就上了

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

    # re: CGLib 學習  回復  更多評論   

    2009-01-16 17:07 by Hill
    請問一下。你真的可以運行嗎?我前幾天做了一個和你這個差不多的。總報錯。請指點一下啊。
    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"
    主站蜘蛛池模板: 久久美女网站免费| 在线观看亚洲AV日韩AV| yellow免费网站| 亚洲性日韩精品国产一区二区| 久久精品国产亚洲av品善| 成年私人影院免费视频网站| 亚洲一级免费毛片| 成全视频在线观看免费高清动漫视频下载| 亚洲国产精品午夜电影| 免费v片在线观看视频网站| 精品亚洲成a人片在线观看| 国产精品久久久久久久久免费| 亚洲黑人嫩小videos| ww在线观视频免费观看| 亚洲xxxxxx| 最好免费观看韩国+日本| 久久亚洲精品无码av| 久久久久国产成人精品亚洲午夜| 国产大片免费天天看| 亚洲免费在线播放| 99在线精品免费视频九九视| 亚洲天堂男人影院| 免费一级做a爰片久久毛片潮喷| 农村寡妇一级毛片免费看视频| 亚洲综合网站色欲色欲| 99精品视频在线视频免费观看 | 拔擦拔擦8x华人免费久久| 美女18毛片免费视频| 亚洲中文字幕无码中文字在线| 久久午夜伦鲁片免费无码| 亚洲欧美日韩中文无线码| 亚洲电影日韩精品| 一区二区三区福利视频免费观看| 亚洲国产精品一区二区三区在线观看 | 永久免费的网站在线观看| 国产精品观看在线亚洲人成网| 夜夜春亚洲嫩草影院| 免费观看黄网站在线播放| eeuss影院免费直达入口| 亚洲欧洲日产专区| 亚洲精品国产福利一二区|