国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Spring Framework中的AOP編程之入門篇

2024-07-21 02:14:58
字體:
來源:轉載
供稿:網友

  作為這個介紹spring框架中的面向方面編程(aspect-oriented programming,aop)的系列的第一部分,本文介紹了使您可以使用spring中的面向方面特性進行快速開發的基礎知識。使用跟蹤和記錄方面(面向方面領域的helloworld)作為例子,本文展示了如何使用spring框架所獨有的特性來聲明切入點和通知以便應用方面。本系列的第二部分將更深入地介紹如何運用spring中的所有通知類型和切入點來實現更實用的方面和面向方面設計模式。

  本文的目的不是要介紹構成模塊化j2ee系統——即spring框架——的所有重要元素,我們將只把注意力放在spring所提供的aop功能上。由于spring的模塊化設計方法,我們可以只使用該框架的aop元素,而無需對構成spring框架的其他模塊做太多考慮。

  在aop方面,spring提供了什么?

  “它的目標不是提供最完善的aop實現(雖然spring aop非常強大);而是要提供aop實現與spring ioc的緊密集成,以便幫助解決企業應用中的常見問題?!?br>
  spring framework參考文檔

  為了實現這個目標,spring框架目前支持一組aop概念,從切入點到通知。本文將展示如何使用spring框架中所實現的如下aop概念:

  通知(advice):如何將before通知、afterreturning通知和afterthrowing通知聲明為bean。

  切入點(pointcut):如何聲明靜態切入點邏輯以將xml spring bean configuration文件中的所有內容聯系在一起。

  advisor:關聯切入點定義與通知bean的方式。

  設置場景:一個簡單的例子應用程序

  “一般而言,spring并不是預描述的。雖然使用好的實踐非常容易,但是它避免強制推行一種特定的方法?!?br>spring framework參考文檔

  要試用spring框架的aop功能,首先我們要創建一個簡單的java應用程序。ibusinesslogic接口和businesslogic類為spring框架中的bean提供了簡易構件塊。雖然該接口對于我們的簡單應用程序邏輯來說不是必需的,但是它是spring框架所推薦的良好實踐。

public interface ibusinesslogic
{
 public void foo();
}

public class businesslogic
implements ibusinesslogic
{
 public void foo()
 {
  system.out.println("inside businesslogic.foo()");
 }
}


  可以編寫mainapplication類,借此練習businesslogic bean的公有方法。

import org.springframework.context.applicationcontext;
import org.springframework.context.support.filesystemxmlapplicationcontext;

public class mainapplication
{
 public static void main(string [] args)
 {
  // read the configuration file
  applicationcontext ctx = new filesystemxmlapplicationcontext("springconfig.xml");

  //instantiate an object
  ibusinesslogic testobject = (ibusinesslogic) ctx.getbean("businesslogicbean");

  // execute the public
  // method of the bean
  testobject.foo();
 }
}


  在businesslogic類及其關聯接口中沒有什么需要注意的。但是,mainapplication類初始化businesslogic對象的方式很有意思。通過使用ctx.getbean("businesslogicbean")調用,mainapplication將加載和管理businesslogic類的bean實例的任務轉交給了spring框架。

  允許spring控制businesslogic bean的初始化,這使得spring運行時有機會在bean被返回給應用程序之前執行j2ee系統所需的所有與bean相關的管理任務。然后spring運行時配置可以決定對bean應用哪些任務和模塊。該配置信息由一個xml文件提供,類似于下面所示的:

<?xml version="1.0" encoding="utf-8"?>
<!doctype beans public
"-//spring//dtd bean//en"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<!-- bean configuration -->
<bean id="businesslogicbean"
class="org.springframework.aop.framework.proxyfactorybean">
<property name="proxyinterfaces">
<value>ibusinesslogic</value>
</property>
<property name="target">
<ref local="beantarget"/>
</property>
</bean>
<!-- bean classes -->
<bean id="beantarget"
class="businesslogic"/>

</beans>


  該配置文件,即springconfig.xml,指定要加載一個接口與ibusinesslogic相匹配的bean。該bean隨后被關聯到businesslogic實現類。看起來好像是費了很大力氣只為了加載一個簡單的bean并調用一個方法,但是您要知道,這個配置文件只是使spring框架可以透明地對應用程序應用其組件的眾多特性的一個體現。

  圖1顯示了基本的順序圖:mainapplication原樣執行,沒有應用方面。


圖1.沒有對businesslogic bean應用方面時的順序圖
  應用方法跟蹤(method tracing)方面

  可能最基本的方面就是方法跟蹤方面了。這可能是您找得到的最簡單的方面了,因此它是研究新的aop實現的一個很好的起點。

  方法跟蹤方面在一個目標應用程序內捕獲對所跟蹤的方法的調用以及方法的返回值,并以某種方式顯示這種信息。在aop中,通知的before和after類型用于捕獲這些類型的聯結點,因為這兩種通知可以在方法調用聯結點之前或之后觸發。使用spring框架,方法跟蹤方面的before通知是在tracingbeforeadvice類中聲明的。

import java.lang.reflect.method;
import org.springframework.aop. methodbeforeadvice;

public class tracingbeforeadvice
implements methodbeforeadvice
{
 public void before(method m, object[] args, object target)
 throws throwable
 {
  system.out.println("hello world! (by " + this.getclass().getname() + ")");
 }
}


  類似地,after通知可以在tracingafteradvice類中聲明。

import java.lang.reflect.method;
import org.springframework.aop.afterreturningadvice;

public class tracingafteradvice
implements afterreturningadvice
{
 public void afterreturning(object object, method m, object[] args, object target)
 throws throwable
 {
  system.out.println("hello world! (by " + this.getclass().getname() + ")");
 }
}


  這兩個類都通過實現spring框架的適當通知接口而表示了特定的通知。每種類型的通知都指定實現before(..)或afterreturning(..)方法,以便使spring運行時可以告訴通知適當的聯結點會在何時出現。值得注意的是,tracingafteradvice實際上是從afterreturningadvice擴展而來的,表示只有在聯結點在無異常的情況下獲得返回值時才運行通知。

  為了將通知與應用程序中的適當聯結點關聯起來,必須對springconfig.xml進行一些修改。

<?xml version="1.0" encoding="utf-8"?>
<!doctype beans public
"-//spring//dtd bean//en"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<!-- bean configuration -->
<bean id="businesslogicbean"
class="org.springframework.aop.framework.proxyfactorybean">
<property name="proxyinterfaces">
<value>ibusinesslogic</value>
</property>
<property name="target">
<ref local="beantarget"/>
</property>
<property name="interceptornames">
<list>
<value>thetracingbeforeadvisor</value>
<value>thetracingafteradvisor</value>
</list>
</property>
</bean>
<!-- bean classes -->
<bean id="beantarget"
class="businesslogic"/>

<!-- advisor pointcut definition for before advice -->
<bean id="thetracingbeforeadvisor"
class="org.springframework.aop.support.regexpmethodpointcutadvisor">
<property name="advice">
<ref local="thetracingbeforeadvice"/>
</property>
<property name="pattern">
<value>.*</value>
</property>
</bean>

<!-- advisor pointcut definition for after advice -->
<bean id="thetracingafteradvisor"
class="org.springframework.aop.support.regexpmethodpointcutadvisor">
<property name="advice">
<ref local="thetracingafteradvice"/>
</property>
<property name="pattern">
<value>.*</value>
</property>
</bean<

<!-- advice classes -->
<bean id="thetracingbeforeadvice"
class="tracingbeforeadvice"/>
<bean id="thetracingafteradvice"
class="tracingafteradvice"/>

</beans>


  thetracingbeforeadvisor和thetracingafteradvisor advisor被添加到前面所聲明的businesslogicbean。每個advisor都可能截獲所有bean所關聯到的聯結點。advisor本身就是bean,而它唯一的作用就是將切入點定義與通知bean關聯起來。本例中的切入點定義是在靜態對象層次結構中指定相關聯結點的正則表達式。

  因為本例中使用了org.springframework.aop.support.regexpmethodpointcutadvisor切入點advisor,切入點邏輯是使用正則表達式指定的。正則表達式用于識別公有接口對ibusinesslogici接口的聯結點。下面是一些可以用來指定ibusinesslogic接口上的不同聯結點集合的正則表達式例子:

<value>.*</value>:該表達式選擇advisor所關聯到的一個或多個bean上的所有聯結點。
<value>./ibusinesslogic/.foo</value>:該表達式只選擇ibusinesslogic接口上的foo()方法的聯結點。如果是advisor所關聯到的bean,則該表達式只選擇ibusinesslogic接口上的聯結點。


  springconfig.xml文件中最后的bean聲明指定實現通知bean的類。

  既然已經指定了跟蹤方面的正確配置,那么下一次執行mainapplication時,這些方面就會在初始化過程中被編織進去,而businesslogic bean中的所有方法都將被跟蹤,如圖2所示。



圖2. 方法跟蹤方面應用到businesslogic bean之后的順序圖


  方法跟蹤方面和例子應用程序的源代碼可在本文末尾的參考資料小節進行下載。

  方面的重用

  可以對方法跟蹤方面進行擴展,提供一個稍微復雜的記錄(logging)方面。記錄方面提供了一個很不錯的重用例子,因為記錄方面所需的許多特性都已經包含在方法跟蹤方面中了。

  在本例中,記錄方面擴展了方法跟蹤方面,以便顯示附加的與(在應用程序的執行過程中)所引發的異常有關的信息。

  要完全使用記錄方面,需要對應用程序做一些更改。businesslogicexception異常類提供了一個可以由ibusinesslogicinterface接口和businesslogic實現類新增的void bar()方法引發的異常。

public class businesslogicexception
extends exception
{}

public interface ibusinesslogic
{
 public void foo();

 public void bar()
 throws businesslogicexception;
}

public class businesslogic
implements ibusinesslogic
{
 public void foo()
 {
  system.out.println("inside businesslogic.foo()");
 }

 public void bar()
 throws businesslogicexception
 {
  system.out.println("inside businesslogic.bar()");
  throw new businesslogicexception();
 }
}


  mainapplication類現在將對void bar()方法進行一次額外的調用,并處理選中的、可能由該方法引發的異常。

import org.springframeworkcontext.applicationcontext;
import org.springframework.context.support.filesystemxmlapplicationcontext;

public class mainapplication
{
 public static void main(string [] args)
 {
  // read the configuration file
  applicationcontext ctx = new filesystemxmlapplicationcontext( "springconfig.xml");

  //instantiate an object
  ibusinesslogic testobject = (ibusinesslogic) ctx.getbean("businesslogicbean");

  //execute the public methods of the bean
  testobject.foo();

  try
  {
   testobject.bar();
  }
  catch(businesslogicexception ble)
  {
   system.out.println("caught businesslogicexception");
  }
 }
}


  來自方法跟蹤方面的tracingbeforeadvice和tracingafteradvice通知可以整體重用。loggingthrowsadvice類為新的異常記錄提供了通知。

import org.springframework.aop.throwsadvice;
import java.lang.reflect.method;

public class loggingthrowsadvice
implements throwsadvice
{
 public void afterthrowing(method method, object[] args, object target, throwable subclass)
 {
  system.out.println("logging that a " + subclass + "exception was thrown.");
 } 
}



  應用記錄方面的最后一步是修改springconfig.xml配置文件,使其包含新添加的loggingthrowsadvice通知。


圖3顯示了運行mainapplication并使用spring框架應用了記錄方面的uml順序圖。

  圖3. 記錄方面應用到businesslogic bean之后的順序圖(單擊圖像查看大圖)   此處的記錄方面清楚地說明了如何重用現有方面以及如何在spring框架中使用通知的throws形式。通過為before和after通知聲明新的通知來重寫現有的方法跟蹤方面實現,可以實現更復雜的記錄方面,記錄到更復雜的記錄框架,比如log4j。關于記錄方面和例子應用程序的源代碼,請參見本文末尾的參考資料小節。

  結束語

  本文展示了使用spring框架中的基本aop結構所應用的一些簡單方面。在本系列的下一篇文章中,我們將介紹一些更實用的方面,探討方面的生命周期,使用spring框架的around通知,并使用spring來應用aop模式。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 方山县| 大石桥市| 阿拉善盟| 平果县| 云龙县| 哈巴河县| 林芝县| 鹤峰县| 漯河市| 蛟河市| 柯坪县| 长沙县| 正阳县| 灯塔市| 察隅县| 广元市| 玛多县| 阿克陶县| 云林县| 东港市| 固镇县| 秦皇岛市| 花莲市| 五大连池市| 酒泉市| 岳阳县| 法库县| 黄平县| 迁安市| 榆社县| 浮山县| 肇州县| 邵武市| 黄石市| 康平县| 常德市| 临潭县| 甘孜| 兰坪| 海原县| 浑源县|