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

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

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

    2007年11月16日

           最近在學習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的權限檢查。

     

    posted @ 2007-11-16 14:06 wigalos 閱讀(40107) | 評論 (14)編輯 收藏

    主站蜘蛛池模板: 久久久久亚洲av无码专区| 亚洲欧洲日产国码无码网站 | 国产男女猛烈无遮档免费视频网站| 亚洲专区在线视频| 久久久久免费看成人影片| 久久亚洲国产精品一区二区| 西西人体免费视频| 亚洲AV无码久久| 日韩插啊免费视频在线观看| 91在线精品亚洲一区二区| 69视频免费观看l| 亚洲一区二区三区无码国产| 永久免费毛片在线播放| 亚洲一区二区观看播放| 亚洲男人的天堂在线va拉文| 久久高潮一级毛片免费| 精品亚洲aⅴ在线观看| 久久久久久久91精品免费观看| 33333在线亚洲| 国产在线ts人妖免费视频| 一级特黄a大片免费| 亚洲精品高清国产一线久久| 最近免费视频中文字幕大全| 亚洲va在线va天堂va手机| 永久免费视频v片www| 精品国产日韩亚洲一区在线| 亚洲欧洲国产精品香蕉网| 最近中文字幕国语免费完整| 亚洲熟妇无码av另类vr影视| 内射无码专区久久亚洲 | 亚洲毛片在线免费观看| 亚洲精品久久久久无码AV片软件| 免费人成视频在线观看视频| 国产午夜成人免费看片无遮挡| 亚洲精品免费在线视频| 国产精品无码免费播放| 4hu四虎免费影院www| 亚洲女人影院想要爱| 亚洲国产成人久久综合野外| 在线免费观看国产| 曰批免费视频播放免费|