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

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

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

    leisure

    JAVA - exceed,helloworld
    隨筆 - 50, 文章 - 0, 評論 - 11, 引用 - 0
    數據加載中……

    spring method interceptor

    spring method interceptor

    -author: leisure.xu

    首先dao里面有find和save方法,本實例以攔截find方法為主,并改變find的返回值。

    package com.leisure;

    public class Dao {

         public String find() {

              System. out.println( "dao: find()");

              return "student";

         }

         public void save() {

              System. out.println( "dao: save()");

         }

    }

    一、新增一個DaoInterceptor,如下

    package com.leisure;

    import org.aopalliance.intercept.MethodInterceptor;

    import org.aopalliance.intercept.MethodInvocation;

    /**

     * class description goes here

     * @author leisure.xu

     * @version 1.0.0, 2012 -6 -29

     */

    public class DaoInterceptor implements MethodInterceptor {

         @Override

         public Object invoke(MethodInvocation invocation) throws Throwable {

              String methodName = invocation.getMethod().getName();

              if( "find".equals(methodName)) {

                   System. out.println( "invocation modify the return result to 'teacher'");

                   return "teacher";

              }

              return invocation.proceed();

         } 

    }

         DaoInterceptor實現了MethodInterceptor的invoke方法,在這里,MethodInvocation參數可以獲取到getArguments等數據,至于能做什么,你懂的。

    二、Dao跟DaoInterceptor還是沒扯上關系,這時需要修改applicationContext.xml

         原來:

         <bean id = "dao" class= "com.leisure.Dao"/>

    修改為:

              <!--

          <bean id=" dao" class="com.leiusre.Dao"/>

         -->

         <bean id ="daoInterceptor" class="com.leisure.DaoInterceptor"/>

         <bean id ="dao" class= "org.springframework.aop.framework.ProxyFactoryBean" >

              <property name ="target">

                   <bean class ="com.leisure.Dao" />

              </property >

              <property name ="interceptorNames">

                   <list >

                        <value >daoInterceptor </value >

                   </list >

              </property >

         </bean >

    三、運行看效果!

         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml" );

         Dao dao = context.getBean(Dao. class);

         System. out.println(dao.find());

         dao.save();

    結果:

    invocation modify the return result to 'teacher'

    teacher

    dao: save()

    從結果可以看出invocation攔截了find方法,并且修改了其返回結果,而對象的find方法并沒有執行到。

    該實例引用到的jar包:




    posted on 2012-07-11 09:14 leisure 閱讀(998) 評論(0)  編輯  收藏 所屬分類: java

    主站蜘蛛池模板: 波多野结衣在线免费视频 | 免费看美女让人桶尿口| 亚洲伦理一区二区| 男人的天堂网免费网站| 亚洲国产精品国自产电影| 午夜视频免费在线观看| 亚洲精品国产成人99久久| 亚洲成人免费电影| 亚洲国产品综合人成综合网站| 亚洲一级毛片免费在线观看| 亚洲免费在线视频播放| 黄瓜视频高清在线看免费下载| 亚洲中文字幕乱码一区| 国产一级大片免费看| 一级看片免费视频囗交| 日本红怡院亚洲红怡院最新 | 高潮内射免费看片| 亚洲七七久久精品中文国产| 久久嫩草影院免费看夜色| 亚洲va无码va在线va天堂| 亚洲三级在线免费观看| 国产成人精品日本亚洲语音| 久久亚洲国产精品五月天婷| 日本亚洲欧洲免费天堂午夜看片女人员 | 亚洲国产精品无码久久久蜜芽| 香蕉成人免费看片视频app下载| 亚洲国产成人久久77| 日韩精品免费一区二区三区| 国产伦精品一区二区免费| 亚洲AV第一页国产精品| 久久久久国产精品免费免费搜索| 国产AV无码专区亚洲AV琪琪| 国产成人麻豆亚洲综合无码精品| 91福利视频免费| 爱情岛论坛亚洲品质自拍视频网站| 国产亚洲欧洲Aⅴ综合一区| 最近在线2018视频免费观看| 黄页网站在线免费观看| 亚洲综合在线视频| 国产午夜无码视频免费网站| 久久精品国产这里是免费|