leisure

          JAVA - exceed,helloworld
          隨筆 - 50, 文章 - 0, 評(píng)論 - 11, 引用 - 0
          數(shù)據(jù)加載中……

          spring method interceptor

          spring method interceptor

          -author: leisure.xu

          首先dao里面有find和save方法,本實(shí)例以攔截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()");

               }

          }

          一、新增一個(gè)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實(shí)現(xiàn)了MethodInterceptor的invoke方法,在這里,MethodInvocation參數(shù)可以獲取到getArguments等數(shù)據(jù),至于能做什么,你懂的。

          二、Dao跟DaoInterceptor還是沒(méi)扯上關(guān)系,這時(shí)需要修改applicationContext.xml

               原來(lái):

               <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 >

          三、運(yùn)行看效果!

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

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

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

               dao.save();

          結(jié)果:

          invocation modify the return result to 'teacher'

          teacher

          dao: save()

          從結(jié)果可以看出invocation攔截了find方法,并且修改了其返回結(jié)果,而對(duì)象的find方法并沒(méi)有執(zhí)行到。

          該實(shí)例引用到的jar包:




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

          主站蜘蛛池模板: 沂水县| 阿巴嘎旗| 济南市| 洛阳市| 临沧市| 南岸区| 鲜城| 东丰县| 伊金霍洛旗| 昌江| 汉中市| 炉霍县| 资阳市| 孟津县| 嘉兴市| 肃宁县| 中超| 遂溪县| 丽水市| 海城市| 武山县| 虞城县| 环江| 溧水县| 崇信县| 高邑县| 邵阳县| 敦煌市| 正镶白旗| 枝江市| 雅安市| 呼图壁县| 报价| 衡水市| 江油市| 新源县| 玛曲县| 礼泉县| 册亨县| 萍乡市| 沙洋县|