作為這個介紹Spring架構中的面向方面編程(Aspect-Oriented Programming,AOP)的系列的第一部分,本文介紹了使您可以使用Spring中的面向方面特性進行快速開發的基礎知識。使用跟蹤和記錄方面(面向方面領域的HelloWorld)作為例子,本文展示了如何使用Spring架構所專屬的特性來聲明切入點和通知以便應用方面。本系列的第二部分將更深入地介紹如何運用Spring中的所有通知類型和切入點來實現更實用的方面和面向方面設計模式。
本文的目的不是要介紹構成模組化J2EE系統——即Spring架構——的所有重要元素,我們將只把注意力放在Spring所提供的AOP功能上。由於Spring的模組化設計方法,我們可以只使用該架構的AOP元素,而無需對構成Spring架構的其他模組做太多考慮。
在AOP方面,Spring提供了什嗎?
“它的目標不是提供最完善的AOP實現(雖然Spring AOP非常強大);而是要提供AOP實現與Spring IoC的緊密整合,以便協助解決公司專屬應用程式中的常見問題。”
Spring Framework參考文檔
為了實現這個目標,Spring架構目前支援一組AOP概念,從切入點到通知。本文將展示如何使用Spring架構中所實現的如下AOP概念:
通知(Advice):如何將before通知、afterReturning通知和afterThrowing通知聲明為bean。
切入點(Pointcut):如何聲明靜態切入點邏輯以將XML Spring Bean Configuration檔案中的所有內容聯絡在一起。
Advisor:關聯切入點定義與通知bean的方式。
設定情境:一個簡單的例子應用程式
“一般而言,Spring並不是預描述的。雖然使用好的實踐非常容易,但是它避免強制推行一種特定的方法。”
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關聯起來。本例中的切入點定義是在靜態對象階層中指定相關連接點的Regex。 因為本例中使用了org.springframework.aop.support.RegexpMethodPointcutAdvisor切入點advisor,切入點邏輯是使用Regex指定的。Regex用於識別公有介面對IbusinessLogici介面的連接點。下面是一些可以用來指定IBusinessLogic介面上的不同連接點集合的Regex例子:
<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模式。 |