Spring 框架的一個關鍵組件是面向方面的編程(AOP)框架。java
面向切面的編程須要把程序邏輯分解成不一樣的部分稱爲所謂的關注點。跨一個應用程序的多個點的功能被稱爲橫切關注點。如日誌記錄、審計、聲明式事務、安全性和緩存等。spring
Spring AOP 模塊提供攔截器來攔截一個應用程序,例如,當執行一個方法時,你能夠在方法執行以前或以後添加額外的功能。express
在咱們開始使用 AOP 工做以前,讓咱們熟悉一下 AOP 概念和術語。這些術語並不特定於 Spring,而是與 AOP 有關的。編程
項 | 描述 |
---|---|
Aspect | 一個模塊具備一組提供橫切需求的 APIs。例如,一個日誌模塊爲了記錄日誌將被 AOP 方面調用。應用程序能夠擁有任意數量的方面,這取決於需求。 |
Join point | 在你的應用程序中它表明一個點,你能夠在插件 AOP 方面。你也能說,它是在實際的應用程序中,其中一個操做將使用 Spring AOP 框架。 |
Advice | 這是實際行動以前或以後執行的方法。這是在程序執行期間經過 Spring AOP 框架實際被調用的代碼。 |
Pointcut | 這是一組一個或多個鏈接點,通知應該被執行。你能夠使用表達式或模式指定切入點正如咱們將在 AOP 的例子中看到的。 |
Introduction | 引用容許你添加新方法或屬性到現有的類中。 |
Target object | 被一個或者多個方面所通知的對象,這個對象永遠是一個被代理對象。也稱爲被通知對象。 |
Weaving | Weaving 把方面鏈接到其它的應用程序類型或者對象上,並建立一個被通知的對象。這些能夠在編譯時,類加載時和運行時完成。 |
爲了在本節的描述中使用aop命名空間標籤,你須要導入spring-aop j架構,以下所述緩存
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!-- bean definition & AOP specific configuration --> </beans>
<aop:config> <aop:aspect id="myAspect" ref="aBean"> ... </aop:aspect> </aop:config> <bean id="aBean" class="..."> ... </bean>
你能夠使用<aop:{ADVICE NAME}>元素在一箇中聲明五個建議中的任何一個,以下所示:安全
<aop:config> <aop:aspect id="myAspect" ref="aBean"> <aop:pointcut id="businessService" expression="execution(* com.xyz.myapp.service.*.*(..))"/> <!-- a before advice definition --> <aop:before pointcut-ref="businessService" method="doRequiredTask"/> <!-- an after advice definition --> <aop:after pointcut-ref="businessService" method="doRequiredTask"/> <!-- an after-returning advice definition --> <!--The doRequiredTask method must have parameter named retVal --> <aop:after-returning pointcut-ref="businessService" returning="retVal" method="doRequiredTask"/> <!-- an after-throwing advice definition --> <!--The doRequiredTask method must have parameter named ex --> <aop:after-throwing pointcut-ref="businessService" throwing="ex" method="doRequiredTask"/> <!-- an around advice definition --> <aop:around pointcut-ref="businessService" method="doRequiredTask"/> ... </aop:aspect> </aop:config> <bean id="aBean" class="..."> ... </bean>
@AspectJ做爲經過Java 5註釋的普通的Java類,它指的是聲明方面的一種風格。經過在你的基於架構的XML配置文件中包含如下元素,@ AspectJ支持是可用的。架構
<aop:aspectj-autoproxy/>
@Aspect public class Logging { //此方法返回一個對象,bean表明切入點 @Pointcut("execution(* com.tutorialspoint.*.*(..))") private void selectAll(){} @Before("selectAll()") public void beforeAdvice(){ System.out.println("Going to setup student profile."); } @After("selectAll()") public void afterAdvice(){ System.out.println("Student profile has been setup."); } @AfterReturning(pointcut = "selectAll()", returning="retVal") public void afterReturningAdvice(Object retVal){ System.out.println("Returning:" + retVal.toString() ); } @AfterThrowing(pointcut = "selectAll()", throwing = "ex") public void AfterThrowingAdvice(IllegalArgumentException ex){ System.out.println("There has been an exception: " + ex.toString()); } }
<aop:aspectj-autoproxy/> <!-- Definition for student bean --> <bean id="student" class="com.tutorialspoint.Student"> <property name="name" value="Zara" /> <property name="age" value="11"/> </bean> <!-- Definition for logging aspect --> <bean id="logging" class="com.tutorialspoint.Logging"/>