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

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

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

    JAVA流通橋

    JAVA啟發者

    統計

    留言簿(3)

    AJAX相關網址

    Eclipse相關網址

    Hibernate

    java相關網址

    LINUX相關網址

    webwork相關網址

    友好鏈接

    閱讀排行榜

    評論排行榜

    Spring AOP介紹

      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 on 2012-06-02 13:53 朱巖 閱讀(173) 評論(0)  編輯  收藏 所屬分類: SPRING文章


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


    網站導航:
     
    主站蜘蛛池模板: 亚洲AV无码一区二区乱子仑| 亚洲色偷偷av男人的天堂| 亚洲欧洲av综合色无码| 免费无码AV片在线观看软件| 亚洲妇女水蜜桃av网网站| 四虎国产成人永久精品免费| 亚洲第一AV网站| 七色永久性tv网站免费看| 亚洲麻豆精品果冻传媒| 2022久久国产精品免费热麻豆| 久久亚洲精品无码aⅴ大香| 无码av免费毛片一区二区| 亚洲高清一区二区三区| 成全视频免费高清| 直接进入免费看黄的网站| 亚洲天堂在线视频| 一级毛片不卡片免费观看| 亚洲黄色三级视频| 日韩激情无码免费毛片| 免费激情网站国产高清第一页| 77777亚洲午夜久久多人| 无码免费一区二区三区免费播放| 亚洲国产精品成人精品小说| 午夜视频在线观看免费完整版| 在线91精品亚洲网站精品成人| 久久久久一级精品亚洲国产成人综合AV区| 巨胸狂喷奶水视频www网站免费| 久久91亚洲精品中文字幕| 国产免费久久精品99re丫y| 美女视频黄频a免费大全视频| 国产v亚洲v天堂无码网站| 国产一卡2卡3卡4卡2021免费观看| 亚洲免费综合色在线视频| 国产精品亚洲综合一区| 24小时在线免费视频| 猫咪免费观看人成网站在线| 久久亚洲一区二区| 国产一级理论免费版| 免费毛片a线观看| 午夜亚洲WWW湿好爽| 亚洲国产美女精品久久久久∴|