前言
上一篇我們介紹了Spring的核心概念DI,DI有助與應(yīng)用對象之間的解耦。今天我們就來介紹下另一個非常核心的概念,面向切面編程AOP。
正文
在軟件開發(fā)中,散布于應(yīng)用中多處的功能被稱為橫切關(guān)注點(diǎn)(cross-cutting concern)。通常來講,這些橫切關(guān)注點(diǎn)從概念上是與應(yīng)用的業(yè)務(wù)邏輯相分離的。比如:日志、聲明式事物、安全和緩存。這些東西都不是我們平時寫代碼的核心功能,但許多地方都要用到。
把這些橫切關(guān)注點(diǎn)與業(yè)務(wù)相分離正是面向切面編程(AOP)索要解決的問題。
簡單的說就是把這些許多地方都要用到,但又不是核心業(yè)務(wù)的功能,單獨(dú)剝離出來封裝,通過配置指定要切入到指定的方法中去。
什么是面向切面編程
如上圖所示,這就是橫切關(guān)注點(diǎn)的概念,水平的是核心業(yè)務(wù),這些切入的箭頭就是我們的橫切關(guān)注點(diǎn)。
橫切關(guān)注點(diǎn)可以被模塊化為特殊的類,這些類被稱為切面(aspect)。這樣做有兩個好處:
- 首先,現(xiàn)在每個關(guān)注點(diǎn)都集中于一個地方,而不是分割到多處代碼中
- 其次,服務(wù)模塊更簡潔,因?yàn)樗鼈冎话饕P(guān)注點(diǎn)(或核心功能)的代碼,而次要關(guān)注點(diǎn)的代碼被轉(zhuǎn)移到切面中了。
定義AOP術(shù)語
為了理解AOP,我們必須先了解AOP的相關(guān)術(shù)語,很簡單不難:
通知(Advice):
在AOP中,切面的工作被稱為通知。通知定義了切面“是什么”以及“何時”使用。除了描述切面要完成的工作,通知還解決了何時執(zhí)行這個工作的問題。
Spring切面可以應(yīng)用5種類型的通知:
- 前置通知(Before):在目標(biāo)方法被調(diào)用之前調(diào)用通知功能
- 后置通知(After):在目標(biāo)方法完成之后調(diào)用通知,此時不會關(guān)心方法的輸出是什么
- 返回通知(After-returning):在目標(biāo)方法成功執(zhí)行之后調(diào)用通知
- 異常通知(After-throwing):在目標(biāo)方法拋出異常后調(diào)用通知
- 環(huán)繞通知(Around):通知包裹了被通知的方法,在被通知的方法調(diào)用之前和調(diào)用之后執(zhí)行自定義的行為
連接點(diǎn)(Join point):
連接點(diǎn)是在應(yīng)用執(zhí)行過程中能夠插入切面的一個點(diǎn)。這個點(diǎn)可以是調(diào)用方法時、拋出異常時、甚至修改一個字段時。切面代碼可以利用這些點(diǎn)插入到應(yīng)用的正常流程之中,并添加行為。
切點(diǎn)(Pointcut):
如果說通知定義了切面“是什么”和“何時”的話,那么切點(diǎn)就定義了“何處”。比如我想把日志引入到某個具體的方法中,這個方法就是所謂的切點(diǎn)。
切面(Aspect):
切面是通知和切點(diǎn)的結(jié)合。通知和切點(diǎn)共同定義了切面的全部內(nèi)容———他是什么,在何時和何處完成其功能。
引入(Introduction):
引入允許我們向現(xiàn)有的類添加新的方法和屬性(Spring提供了一個方法注入的功能)。
織入(Weaving):
把切面應(yīng)用到目標(biāo)對象來創(chuàng)建新的代理對象的過程,織入一般發(fā)生在如下幾個時機(jī):
- 編譯時:當(dāng)一個類文件被編譯時進(jìn)行織入,這需要特殊的編譯器才可以做的到,例如AspectJ的織入編譯器
- 類加載時:使用特殊的ClassLoader在目標(biāo)類被加載到程序之前增強(qiáng)類的字節(jié)代碼
- 運(yùn)行時:切面在運(yùn)行的某個時刻被織入,SpringAOP就是以這種方式織入切面的,原理應(yīng)該是使用了JDK的動態(tài)代理技術(shù)
Spring對AOP的支持
創(chuàng)建切入點(diǎn)來定義切面所織入的連接點(diǎn)是AOP框架的基本功能。
Spring提供了4種類型的AOP支持:
- 基于代理的經(jīng)典Spring AOP
- 純POJO切面
- @AspectJ注解驅(qū)動的切面
- 注入式AspectJ切面(使用與Spring各版本)
前三種都是Spring AOP實(shí)現(xiàn)的變體,Spring AOP構(gòu)建在動態(tài)代理基礎(chǔ)之上,因此,Spring對AOP的支持局限于方法攔截。
這里我不準(zhǔn)備介紹經(jīng)典Spring AOP,因?yàn)橐肓撕唵蔚穆暶魇紸OP和基于直接的AOP后,Spring經(jīng)典的AOP看起來就顯得非常笨重和過于復(fù)雜。
對于新手入門來說,我們不需要知道這么多,在這里我也只介紹2,3兩種方式,簡單的說就是一個基于xml配置,一個基于注解。
下面就直接開始舉兩個例子分別來介紹下這兩種AOP方式,我們就拿簡單的日志來說明。
基于注解的方式
首先基于注解的方式需要引入這些包,對用的pom.xml如下:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.8</version>
</dependency>
我們還是舉前面用到的UserController來說明,下面方法很簡單,執(zhí)行進(jìn)入這個方法的時候會打印“進(jìn)來了”信息,現(xiàn)在我打算給這個方法加日志,在執(zhí)行該方法前打印“進(jìn)來前”,在執(zhí)行完方法后執(zhí)行“進(jìn)來后”。
package com.tengj.demo.controller;
@Controller
@RequestMapping(value="/test")
public class UserController {
@Autowired
UserService userService;
@RequestMapping(value="/view",method = RequestMethod.GET)
public String index(){
userService.sayHello("tengj");
return "index";
}
}
servie層代碼:
package com.tengj.demo.service
public interface UserService {
public void sayHello(String name);
}
servie實(shí)現(xiàn)類代碼:
package com.tengj.demo.service.impl;
@Service("userService")
public class UserServiceImpl implements UserService{
@Override
public void sayHello(String name) {
System.out.println("hello,"+name);
}
}
上面方法index()其實(shí)就是我們之前定義的切點(diǎn),表示在哪里切入AOP。
如圖所示,我們使用execution()指示器選擇UserServiceImpl的sayHello方法。方法表達(dá)式以“*”號開始,表明了我們不關(guān)心方法返回值的類型。然后,我們指定了全限定類名和方法名。對于方法參數(shù)列表,我們使用兩個點(diǎn)號(..)表明切點(diǎn)要選擇任意的sayHello()方法,無論該方法的入?yún)⑹鞘裁础?/p>
接下來我們要定義個切面,也就是所謂的日志功能的類。
package com.tengj.demo.aspect;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component //注入依賴
@Aspect //該注解標(biāo)示該類為切面類
public class LogAspect {
@Pointcut("execution(* com.tengj.demo.service.impl.UserServiceImpl.*(..))")
public void logAop(){}
@Before("logAop() && args(name)")
public void logBefore(String name){
System.out.println(name+"前置通知Before");
}
@AfterReturning("logAop()")
public void logAfterReturning(){
System.out.println("返回通知AfterReturning");
}
@After("logAop() && args(name)")
public void logAfter(String name){
System.out.println(name+"后置通知After");
}
@AfterThrowing("logAop()")
public void logAfterThrow(){
System.out.println("異常通知AfterThrowing");
}
}
上面就是切面類的代碼,很簡單,這里用到了前面提的通知的幾種類型。
這樣就能實(shí)現(xiàn)切入功能了
@Pointcut("execution(* com.tengj.demo.service.impl.UserServiceImpl.*(..))")
public void logAop(){}
這里的@Pointcut注解是為了定義切面內(nèi)重用的切點(diǎn),也就是說把公共的東西抽出來,定義了任意的方法名稱logAop,這樣下面用到的各種類型通知就只要寫成
@Before("logAop() && args(name)")
@AfterReturning("logAop()")
@AfterThrowing("logAop()")
這樣既可,否則就要寫成
@Before("execution(* com.tengj.demo.service.impl.UserServiceImpl.*(..))")
@AfterReturning("execution(* com.tengj.demo.service.impl.UserServiceImpl.*(..))")
@AfterThrowing("execution(* com.tengj.demo.service.impl.UserServiceImpl.*(..))")
大家是否注意到了@Before("logAop() && args(name)")
這里多出來個&& args(name)
,這個是用來傳遞參數(shù)的,定義只要跟sayHello參數(shù)名稱一樣就可以。
如果就此止步的話,LogAspect只會是Spring容器中的一個Bean,即便使用了AspectJ注解,但它并不會被視為切面,這些注解不會解析,也不會創(chuàng)建將其轉(zhuǎn)換為切面的代理。
所以需要在XML里面配置一下,需要使用Spring aop命名空間中的<aop:aspectj-autoproxy/>
元素,簡單如下:
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"
default-lazy-init="true">
<context:component-scan base-package="com.tengj.demo"/>
<mvc:resources location="/WEB-INF/pages/" mapping="/pages/**"/>
<!-- 默認(rèn)的注解映射的支持 -->
<mvc:annotation-driven/>
<!--啟用AspectJ自動代理-->
<aop:aspectj-autoproxy/>
<!-- 視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
接著就可以啟動工程,訪問index這個方法,http://localhost:8080/SpringMVCMybatis/test/view
執(zhí)行結(jié)果:
tengj前置通知Before
hello,tengj
tengj后置通知After
返回通知AfterReturning
根據(jù)前面學(xué)的我們知道,除了上面提到的通知外,還有一個更強(qiáng)大通知類型,就是環(huán)繞通知??梢宰远x我們需要切入的位置,可以替代上面提到的所有通知??蠢樱?/p>
@Around("logAop()")
public void logAround(ProceedingJoinPoint jp){
try {
System.out.println("自定義前置通知Before");
jp.proceed();//將控制權(quán)交給被通知的方法,也就是執(zhí)行sayHello方法
System.out.println("自定義后置通知After");
} catch (Throwable throwable) {
System.out.println("異常處理~");
throwable.printStackTrace();
}
}
執(zhí)行結(jié)果:
自定義前置通知Before
hello,tengj
自定義后置通知After
這里主要是通過ProceedingJoinPoint這個參數(shù)。其中里面的proceed()方法就是將控制權(quán)交給被通知的方法。如果你忘記調(diào)用這個方法,那么你的通知實(shí)際上會阻塞對被通知方法的調(diào)用。
有意思的是,你可以不調(diào)用proceed()方法,從而阻塞堆被通知方法的訪問,與之類似,你也可以在通知中對它進(jìn)行多次調(diào)用。要這樣做的一個場景就是實(shí)現(xiàn)重試邏輯,也就是在被通知方法失敗后,進(jìn)行重復(fù)嘗試。
基于XML配置的方式
這里介紹使用XML配置的方式來實(shí)現(xiàn),在Spring的aop命名空間中,提供了多個元素用來在XML中聲明切面。
AOP配置元素 | 用 途 |
---|---|
<aop:advisor> |
定義AOP通知器 |
<aop:after> |
定義AOP后置通知(不管被通知的方法是否執(zhí)行成功) |
<aop:after-returning> |
定義AOP返回通知 |
<aop:after-throwing> |
定義AOP異常通知 |
<aop:around> |
定義AOP環(huán)繞通知 |
<aop:aspect> |
定義一個切面 |
<aop:aspectj-autoproxy> |
啟用@AspectJ注解驅(qū)動的切面 |
<aop:before> |
定義一個AOP前置通知 |
<aop:config> |
頂層的AOP配置元素,大多數(shù)的<aop:*> 元素必須包含在<aop:config> 元素內(nèi) |
<aop:declare-parents> |
以透明的方式為被通知的對象引入額外的接口 |
<aop:pointcut> |
定義一個切點(diǎn) |
我們已經(jīng)看過了<aop:aspectj-autoproxy/>
元素,它能夠自動代理AspectJ注解的通知類。aop命名空間的其他元素能夠讓我們直接在Spring配置中聲明切面,而不需要使用注解。
所以,我們重新來看看一下這個LogAspect類,這次我們將它所有的AspectJ注解全部移除掉:
package com.tengj.demo.aspect;
public class LogAspect {
public void logBefore(String name){
System.out.println(name+"前置通知Before");
}
public void logAfterReturning(String name){
System.out.println("返回通知AfterReturning");
}
public void logAfter(String name){
System.out.println(name+"后置通知After");
}
public void logAfterThrow(String name){
System.out.println("異常通知AfterThrowing");
}
}
然后在xml配置文件中使用Spring aop命名空間中的一些元素,詳細(xì)基本配置參考上面注解方式中的xml配置,這里是貼出來關(guān)鍵的代碼:
<bean id="logAspect" class="com.tengj.demo.aspect.LogAspect" />
<aop:config>
<aop:aspect id="log" ref="logAspect">
<aop:pointcut id="logAop" expression="execution(* com.tengj.demo.service.impl.UserServiceImpl.sayHello(..)) and args(name)"/>
<aop:before method="logBefore" pointcut-ref="logAop"/>
<aop:after method="logAfter" pointcut-ref="logAop"/>
<aop:after-returning method="logAfterReturning" pointcut-ref="logAop"/>
<aop:after-throwing method="logAfterThrow" pointcut-ref="logAop"/>
<!--<aop:around method="logAfterThrow" pointcut-ref="logAop"/>-->
</aop:aspect>
</aop:config>
配置也 很好理解
- xml里面配置aop,都是放在
<aop:config>
里面 - 然后使用
<aop:aspect>
一個切面,指向具體的bean類。 - 使用
<aop:pointcut>
定義切點(diǎn),基本跟注解的很像,其中要注意的是xml配置里面如果要帶參數(shù)的,用的不再是&&,要使用and關(guān)鍵字才行(因?yàn)樵赬ML中,“&”符號會被解析為實(shí)體的開始) - 然后就是使用各種通知標(biāo)簽了,簡單。
執(zhí)行效果如下:
tengj前置通知Before
hello,tengj
tengj后置通知After
返回通知AfterReturning
環(huán)繞通知也很簡單,直接貼代碼:
xml配置:
<aop:around method="logAround" pointcut-ref="logAop"/>
切面方法:
public void logAround(ProceedingJoinPoint jp,String name){
try {
System.out.println(name+"自定義前置通知Before");
jp.proceed();
System.out.println(name+"自定義后置通知After");
} catch (Throwable throwable) {
System.out.println("異常處理~");
throwable.printStackTrace();
}
}
執(zhí)行結(jié)果:
tengj自定義前置通知Before
hello,tengj
tengj自定義后置通知After
總結(jié)
Spring AOP是Spring學(xué)習(xí)中最關(guān)鍵的,我總結(jié)的這2種寫法也是開發(fā)中最常用的。也不知道大家能不能理解~看得時候如果有不懂的地方可以提出來,我好修改一下,讓更多的人理解并掌握AOP,希望對你有所幫助。
一直覺得自己寫的不是技術(shù),而是情懷,一篇篇文章是自己這一路走來的痕跡。靠專業(yè)技能的成功是最具可復(fù)制性的,希望我的這條路能讓你少走彎路,希望我能幫你抹去知識的蒙塵,希望我能幫你理清知識的脈絡(luò),希望未來技術(shù)之巔上有你也有我。