??? AOP正在成為軟件開發的下一個圣杯。使用AOP,你可以將處理aspect的代碼注入主程序,通常主程序的主要目的并不在于處理這些aspect。AOP可以防止代碼混亂。
??? 為了理解AOP如何做到這點,考慮一下記日志的工作。日志本身不太可能是你開發的主程序的主要任務。如果能將“不可見的”、通用的日志代碼注入主程序中,那該多好啊。AOP可以幫助你做到。
??? Spring framework是很有前途的AOP技術。作為一種非侵略性的,輕型的AOP framework,你無需使用預編譯器或其他的元標簽,便可以在Java程序中使用它。這意味著開發團隊里只需一人要對付AOP framework,其他人還是象往常一樣編程。
??? AOP是很多直覺難以理解的術語的根源。幸運的是,你只要理解三個概念,就可以編寫AOP模塊。這三個概念是:advice,pointcut和 advisor。advice是你想向別的程序內部不同的地方注入的代碼。pointcut定義了需要注入advice的位置,通常是某個特定的類的一個 public方法。advisor是pointcut和advice的裝配器,是將advice注入主程序中預定義位置的代碼。
??? 既然我們知道了需要使用advisor向主要代碼中注入“不可見的”advice,讓我們實現一個Spring AOP的例子。在這個例子中,我們將實現一個before advice,這意味著advice的代碼在被調用的public方法開始前被執行。以下是這個before advice的實現代碼:
??? 代碼:
package com.company.springaop.test;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class TestBeforeAdvice implements MethodBeforeAdvice {
public void before(Method m, Object[] args, Object target)
throws Throwable {
? System.out.println("Hello world! (by "
??? + this.getClass().getName()
??? + ")");
}
}
??? 接口MethodBeforeAdvice只有一個方法before需要實現,它定義了advice的實現。before方法共用三個參數,它們提供了相當豐富的信息。參數Method m是advice開始后執行的方法。方法名稱可以用作判斷是否執行代碼的條件。Object[] args是傳給被調用的public方法的參數數組。當需要記日志時,參數args和被執行方法的名稱,都是非常有用的信息。你也可以改變傳給m的參數,但要小心使用這個功能;編寫最初主程序的程序員并不知道主程序可能會和傳入參數的發生沖突。Object target是執行方法m對象的引用。
??? 在下面的BeanImpl類中,每個public方法調用前,都會執行advice:
??? 代碼:
package com.company.springaop.test;
public class BeanImpl implements Bean {
public void theMethod() {
? System.out.println(this.getClass().getName()
??? + "." + new Exception().getStackTrace()[0].getMethodName()
??? + "()"
??? + " says HELLO!");
}
}
??? 類BeanImpl實現了下面的接口Bean:
??? 代碼:
package com.company.springaop.test;
public interface Bean {
public void theMethod();
}
??? 雖然不是必須使用接口,但面向接口而不是面向實現編程是良好的編程實踐,Spring也鼓勵這樣做。
??? pointcut和advice通過配置文件來實現,因此,接下來你只需編寫主方法的Java代碼:
??? 代碼:
package com.company.springaop.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Main {
public static void main(String[] args) {
? //Read the configuration file
? ApplicationContext ctx = new FileSystemXmlApplicationContext("springconfig.xml");
? //Instantiate an object
? Bean x = (Bean) ctx.getBean("bean");
? //Execute the public method of the bean (the test)
? x.theMethod();
}
}
??? 我們從讀入和處理配置文件開始,接下來馬上要創建它。這個配置文件將作為粘合程序不同部分的“膠水”。讀入和處理配置文件后,我們會得到一個創建工廠ctx。任何一個Spring管理的對象都必須通過這個工廠來創建。對象通過工廠創建后便可正常使用。
??? 僅僅用配置文件便可把程序的每一部分組裝起來。
??? 代碼: