Spring AOP介紹

          Posted on 2007-11-16 14:06 wigalos 閱讀(40107) 評論(14)  編輯  收藏

                 最近在學習spring,做了一些關于Spring AOP的總結和例子,參考書籍是李剛老師著的《輕量級J2EE企業應用實戰》。如下:

                                                       Spring AOP介紹

                 Spring的AOP是上面代理模式的深入。使用Spring AOP,開發者無需實現業務邏輯對象工廠,無需實現代理工廠,這兩個工廠都由Spring容器充當。Spring AOP不僅允許使用XML文件配置目標方法,ProxyHandler也允許使用依賴注入管理,Spring AOP提供了更多靈活的選擇。
          在下面Spring AOP的示例中,InvocationHandler采用動態配置,需要增加的方法也采用動態配置,一個目標對象可以有多個攔截器(類似于代理模式中的代理處理器)。
          下面是原始的目標對象:
          //目標對象的接口
          public interface  Person
          {
           //該接口聲明了兩個方法
           void info();
           void run();
          }
          下面是原始目標對象的實現類,實現類的代碼如下:
          //目標對象的實現類,實現類實現Person接口
          public class PersonImpl implements Person
          {
           //兩個成員屬性
           private String name;
           private int age;
           //name屬性的 setter方法
           public void setName(String name)
           {
            this.name = name;
           }
           //age屬性的setter方法
          public void setAge(int age)
           {
            this.age = age;
           }
           //info方法,該方法僅僅在控制臺打印一行字符串
           public void info()
           {
            System.out.println("我的名字是:  " + name + " , 今年年齡為:  " + age);
           }
           //run方法,該方法也在控制臺打印一行字符串。
           public void run()
           {
            if (age < 45)
            {
             System.out.println("我還年輕,奔跑迅速...");
            }
            else
            {
             System.out.println("我年老體弱,只能慢跑...");
            }
           }
          }
          該Person實例將由Spring容器負責產生和管理,name屬性和age屬性也采用依賴注入管理。
          為了充分展示Spring AOP的功能,此處為Person對象創建三個攔截器。第一個攔截器是調用方法前的攔截器,代碼如下:
          //調用目標方法前的攔截器,攔截器實現MethodBeforeAdvice接口
          public class MyBeforeAdvice implements MethodBeforeAdvice
          {
           //實現MethodBeforeAdvice接口,必須實現before方法,該方法將在目標
           //方法調用之前,自動被調用。
               public void before(Method m, Object[] args, Object target) throws Throwable
           {
            System.out.println("方法調用之前...");
            System.out.println("下面是方法調用的信息:");
            System.out.println("所執行的方法是:" + m);
            System.out.println("調用方法的參數是:" + args);
            System.out.println("目標對象是:" + target);
               }
          }
          第二個攔截器是方法調用后的攔截器,該攔截器將在方法調用結束后自動被調用,攔截器代碼如下:
          //調用目標方法后的攔截器,該攔截器實現AfterReturningAdvice接口
          public class MyAfterAdvice implements AfterReturningAdvice
          {
           //實現AfterReturningAdvice接口必須實現afterReturning方法,該方法將在目標方法
           //調用結束后,自動被調用。
               public void afterReturning(Object returnValue, Method m, Object[] args, Object target)throws Throwable
           {
            System.out.println("方法調用結束...");
          System.out.println("目標方法的返回值是 : " + returnValue);
            System.out.println("目標方法是 : " + m);
            System.out.println("目標方法的參數是 : " + args);
            System.out.println("目標對象是 : " + target);
              }
          }
          第三個攔截器是是Around攔截器,該攔截器既可以在目標方法之前調用,也可以在目標方法調用之后被調用。下面是Around攔截器的代碼:
          //Around攔截器實現MethodInterceptor接口
          public class MyAroundInterceptor implements MethodInterceptor
          {
           //實現MethodInterceptor接口必須實現invoke方法
               public Object invoke(MethodInvocation invocation) throws Throwable
           {
            //調用目標方法之前執行的動作
                   System.out.println("調用方法之前: invocation對象:[" + invocation + "]");
            //調用目標方法
                   Object rval = invocation.proceed();
            //調用目標方法之后執行的動作
                   System.out.println("調用結束...");
                   return rval;
              }
          }
          利用Spring AOP框架,實現之前的代理模式相當簡單。只需要實現對應的攔截器即可,無需創建自己的代理工廠,只需采用Spring容器作為代理工廠。下面在Spring配置文件中配置目標bean,以及攔截器。
          下面是Spring配置文件的代碼:
          <?xml version="1.0" encoding="gb2312"?>
          <!--  Spring配置文件的文件頭-->
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
           "http://www.springframework.org/dtd/spring-beans.dtd">
          <!--  Spring配置文件的根元素-->
          <beans>
           <!--  配置目標對象-->
           <bean id="personTarget" class="lee.PersonImpl">
            <!--  為目標對象注入name屬性值-->
            <property name="name">
             <value>Wawa</value>
            </property>
            <!--  為目標對象注入age屬性值-->
            <property name="age">
             <value>51</value>
            </property>
           </bean>
           <!--  第一個攔截器-->
           <bean id="myAdvice" class="lee.MyBeforeAdvice"/>
           <!--  第二個攔截器-->
           <bean id="myAroundInterceptor" class="lee.MyAroundInterceptor"/>
          <!--  將攔截器包裝成Advisor,該對象還確定代理對怎樣的方法增加處理-->
           <bean id="runAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            <!--  advice屬性確定處理bean-->
            <property name="advice">
             <!-- 此處的處理bean定義采用嵌套bean,也可引用容器的另一個bean-->
             <bean class="lee.MyAfterAdvice"/>
            </property>
            <!--  patterns確定正則表達式模式-->
            <property name="patterns">
             <list>
              <!--  確定正則表達式列表-->
              <value>.*run.*</value>
             </list>
            </property>
           </bean>
           <!--  使用ProxyFactoryBean 產生代理對象-->
           <bean id="person" class="org.springframework.aop.framework.ProxyFactoryBean">
            <!--  代理對象所實現的接口-->
            <property name="proxyInterfaces">
             <value>lee.Person</value>
            </property>
            <!--  設置目標對象-->
            <property name="target">
             <ref local="personTarget"/>  
            </property>
            <!--  代理對象所使用的攔截器-->
            <property name="interceptorNames">
             <list>
              <value>runAdvisor</value>
              <value>myAdvice</value>
              <value>myAroundInterceptor</value>
             </list>
            </property>
           </bean>
          </beans>
          該配置文件使用ProxyFactoryBean來生成代理對象,配置ProxyFactoryBean工廠bean時,指定了target屬性,該屬性值就是目標對象,該屬性值為personTarget,指定代理的目標對象為personTarget。通過interceptorNames屬性確定代理需要的攔截器,攔截器可以是普通的Advice,普通Advice將對目標對象的所有方法起作用,攔截器也可以是Advisor,Advisor是Advice和切面的組合,用于確定目標對象的哪些方法需要增加處理,以及怎樣的處理。在上面的配置文件中,使用了三個攔截器,其中myAdvice、myAroundInterceptor都是普通Advice,它們將對目標對象的所有方法起作用。而runAdvisor則使用了正則表達式切面,匹配run方法,即該攔截器只對目標對象的run方法起作用。

          下面是測試代理的主程序:
          public class BeanTest
          {
              public static void main(String[] args)throws Exception
          {
            //創建Spring容器
          ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");
            //獲取代理對象
            Person p = (Person)ctx.getBean("person");
            //執行info方法
            p.info();
                   System.out.println("===========================================");
            //執行run方法
            p.run();
              }
          }
          下面是程序的執行結果:
          方法調用之前...
          下面是方法調用的信息:
          所執行的方法是:public abstract void lee.Person.info()
          調用方法的參數是:null
          目標對象是:lee.PersonImpl@b23210
          調用方法之前: invocation對象:[invocation: method 'info', arguments
          []; target is of class [lee.PersonImpl]]
          我的名字是:  Wawa , 今年年齡為:  51
          調用結束...
          ===========================================
          方法調用之前...
          下面是方法調用的信息:
          所執行的方法是:public abstract void lee.Person.run()
          調用方法的參數是:null
          目標對象是:lee.PersonImpl@b23210
          調用方法之前: invocation對象:[invocation: method 'run', arguments [
          ]; target is of class [lee.PersonImpl]]
          我年老體弱,只能慢跑...
          調用結束...
          方法調用結束...
          目標方法的返回值是 : null
          目標方法是 : public abstract void lee.Person.run()
          目標方法的參數是 : null
          目標對象是 : lee.PersonImpl@b23210
          程序的執行結果中一行“=”用于區分兩次調用的方法。在調用info方法時,只有myAdvice和myAroundInterceptor兩個攔截器起作用,調用run方法時候,三個攔截器都起作用了。

                 通過上面的介紹,可看出Spring的AOP框架是對代理模式簡化,并拓展了代理模式的使用。
          Spring AOP是Spring聲明式事務的基礎。了解Spring AOP對深入理解Spring的聲明式事務管理是非常有好處的。Spring AOP還可以完成很多功能,例如基于AOP的權限檢查。

           

          Feedback

          # re: Spring AOP介紹  回復  更多評論   

          2008-06-11 16:32 by 路過
          不錯

          # re: Spring AOP介紹  回復  更多評論   

          2008-12-17 14:06 by javaTom
          很好,很強打!
          謝謝

          # re: Spring AOP介紹  回復  更多評論   

          2009-03-27 17:22 by 羅技
          很差!

          # re: Spring AOP介紹[未登錄]  回復  更多評論   

          2010-12-10 11:15 by y
          很詳細

          # re: Spring AOP介紹  回復  更多評論   

          2011-03-04 13:41 by minJack
          謝謝樓主.

          # re: Spring AOP介紹  回復  更多評論   

          2012-02-06 10:52 by zyh
          言簡意賅 很棒

          # re: Spring AOP介紹[未登錄]  回復  更多評論   

          2012-02-24 09:57 by june
          通俗易懂,謝謝

          # re: Spring AOP介紹  回復  更多評論   

          2012-05-02 18:01 by ityangba
          要是能排下版就好,看著太費勁了

          # re: Spring AOP介紹  回復  更多評論   

          2012-11-23 15:03 by yryyr
          ryry

          # re: Spring AOP介紹  回復  更多評論   

          2013-01-11 13:56 by xm

          # re: Spring AOP介紹  回復  更多評論   

          2013-07-16 15:48 by zhong2lian
          @路過
          程序你運行了嗎?難道測試程序獲取的那個bean 能切入嗎?

          # re: Spring AOP介紹  回復  更多評論   

          2013-07-19 11:37 by www.dsprint.cn(武漢畫冊)
          還不錯

          # re: Spring AOP介紹  回復  更多評論   

          2013-07-19 11:41 by www.dsprint.cn(武漢印刷)
          nobad

          # re: Spring AOP介紹  回復  更多評論   

          2013-07-19 14:13 by www.dsprint.cn(武漢印刷)
          很好

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


          網站導航:
           
          主站蜘蛛池模板: 雅安市| 陕西省| 休宁县| 荃湾区| 友谊县| 台东县| 永康市| 潮州市| 金湖县| 旺苍县| 重庆市| 扶风县| 桃江县| 赫章县| 吴川市| 绍兴县| 吉隆县| 德化县| 民乐县| 临夏县| 广东省| 怀仁县| 花垣县| 白山市| 锦屏县| 大厂| 仁寿县| 岢岚县| 库尔勒市| 石首市| 张掖市| 富宁县| 酉阳| 昆明市| 渝北区| 长汀县| 鹤岗市| 抚宁县| 泽普县| 南雄市| 军事|