測試同學(xué)上手Spring 之AOP最易懂的解析
前面連續(xù)介紹了幾篇上手Spring的基礎(chǔ)文章
- 測試同學(xué)從0到1上手Spring
- 測試同學(xué)上手Spring 之IoC深入解析
- 測試同學(xué)上手Spring 之DI深入解析
AOP解析
今天來介紹Spring的另一個(gè)核心技術(shù)點(diǎn)AOP,AOP的概念不好理解,希望大家仔細(xì)閱讀文章并按照文章中的代碼進(jìn)行練習(xí),屆時(shí)一定會(huì)有很大的收獲!
AOP (Aspect OrientProgramming),直譯過來就是 面向切面編程。AOP 是一種編程思想,是面向?qū)ο缶幊?OOP)的一種補(bǔ)充。面向?qū)ο缶幊虒⒊绦虺橄蟪筛鱾€(gè)層次的對象,而面向切面編程是將程序抽象成各個(gè)切面。從《Spring實(shí)戰(zhàn)(第4版)》圖書中扒了一張圖:
從該圖可以很形象地看出,所謂切面,相當(dāng)于應(yīng)用對象間的橫切點(diǎn),我們可以將其單獨(dú)抽象為單獨(dú)的模塊。
Spring提供了面向切面編程的豐富支持,是通過動(dòng)態(tài)代理實(shí)現(xiàn)的。允許通過分離應(yīng)用的業(yè)務(wù)邏輯與系統(tǒng)級服務(wù)(例如:審計(jì)(auditing)和事務(wù)(transaction)管理)進(jìn)行內(nèi)聚性的開發(fā)。應(yīng)用對象只實(shí)現(xiàn)它們應(yīng)該做的——完成業(yè)務(wù)邏輯——僅此而已。它們并不負(fù)責(zé)(甚至是意識(shí)到)其它系統(tǒng)級別的關(guān)注點(diǎn),例如:日志或事務(wù)支持。
AOP 要達(dá)到的效果是,保證開發(fā)者在不修改源代碼的前提下,去為系統(tǒng)中的業(yè)務(wù)組件添加某種通用功能。
AOP基本運(yùn)行流程如下圖所示:
AOP 領(lǐng)域中的特性術(shù)語:
- 橫切關(guān)注點(diǎn):跨越應(yīng)用程序多個(gè)模塊的方法或功能。即是與我們業(yè)務(wù)邏輯無關(guān)的,但是我們需要關(guān)注的部分,就是橫切關(guān)注點(diǎn)。如日志 , 安全 , 緩存 , 事務(wù)等等....
- 切面(ASPECT):橫切關(guān)注點(diǎn)被模塊化的特殊對象。即,它是一個(gè)類。
- 通知(Advice):AOP 框架中的增強(qiáng)處理。通知描述了切面何時(shí)執(zhí)行以及如何執(zhí)行增強(qiáng)處理。它是類中的一個(gè)方法。
- 目標(biāo)(Target):被通知對象。
- 代理(Proxy):向目標(biāo)對象應(yīng)用通知之后創(chuàng)建的對象。
- 連接點(diǎn)(JointPoint):表示應(yīng)用執(zhí)行過程中能夠插入切面的一個(gè)點(diǎn),這個(gè)點(diǎn)可以是方法的調(diào)用、異常的拋出。在 Spring AOP 中,連接點(diǎn)總是方法的調(diào)用。
- 切入點(diǎn)(PointCut):可以插入增強(qiáng)處理的連接點(diǎn)。
- 引入(Introduction):引入允許我們向現(xiàn)有的類添加新的方法或者屬性。
- 織入(Weaving): 將增強(qiáng)處理添加到目標(biāo)對象中,并創(chuàng)建一個(gè)被增強(qiáng)的對象,這個(gè)過程就是織入。
Advice通知
通知(Advice)是切面的一種實(shí)現(xiàn),可以完成簡單織入功能(織入功能就是在這里完成的)。Spring AOP 中有 5 中通知類型,分別如下:
各個(gè)通知的執(zhí)行順序如下圖所示:
實(shí)例編碼
需求:在類中添加日志功能,如下圖:
實(shí)現(xiàn)方法1:在各個(gè)類中添加方法logMsg()。如果類數(shù)量少,問題不大,如果有幾百個(gè)類需要處理,那么工作量就很大了。
實(shí)現(xiàn)方法2:通過aop來實(shí)現(xiàn)
首先,mvn中添加配置
實(shí)例如下:
創(chuàng)建接口
- public interface UserService {
- public void add();
- public void delete();
- public void update();
- public void search();
- }
創(chuàng)建切入點(diǎn)類
- public class UserServiceImpl implements UserService {
- public void add() {
- System.out.println("增加用戶");
- }
- public void delete() {
- System.out.println("刪除用戶");
- }
- public void update() {
- System.out.println("更新用戶");
- }
- public void search() {
- System.out.println("查詢用戶");
- }
創(chuàng)建類,實(shí)現(xiàn)@Before通知
- import java.lang.reflect.Method;
- import org.springframework.aop.MethodBeforeAdvice;
- public class BeforeLog implements MethodBeforeAdvice {
- //method : 要執(zhí)行的目標(biāo)對象的方法
- //objects : 被調(diào)用的方法的參數(shù)
- //Object : 目標(biāo)對象
- public void before(Method method, Object[] objects, Object o) throws Throwable {
- System.out.println( o.getClass().getName() + "的" + method.getName() + "方法被執(zhí)行了");
- }
- }
創(chuàng)建類,實(shí)現(xiàn)@After通知
- import java.lang.reflect.Method;
- import org.springframework.aop.AfterReturningAdvice;
- public class AfterLog implements AfterReturningAdvice {
- //returnValue 返回值
- //method被調(diào)用的方法
- //args被調(diào)用的方法的對象的參數(shù)
- //target 被調(diào)用的目標(biāo)對象
- public void afterReturning(Object returnValue,Method method, Object[] args, Object target) throws Throwable {
- System.out.println("執(zhí)行了" + target.getClass().getName()
- +"的"+method.getName()+"方法,"
- +"返回值:"+returnValue);
- }
- }
編輯xml文件
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:c="http://www.springframework.org/schema/c"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- https://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- https://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd">
- <context:annotation-config/>
- <!--注冊bean-->
- <beanid="userService"class="com.my.demo.aop.UserServiceImpl"/>
- <beanid="beforelog"class="com.my.demo.aop.BeforeLog"/>
- <beanid="afterLog"class="com.my.demo.aop.AfterLog"/>
- <aop:config>
- <!--切入點(diǎn) expression:表達(dá)式匹配要執(zhí)行的方法-->
- <aop:pointcutid="pointcut"expression="execution(*
- com.my.demo.aop.UserServiceImpl.*(..))"/>
- <!--執(zhí)行環(huán)繞; advice-ref執(zhí)行方法.pointcut-ref切入點(diǎn)-->
- <aop:advisoradvice-ref="beforelog"pointcut-ref="pointcut"/>
- <aop:advisoradvice-ref="afterLog"pointcut-ref="pointcut"/>
- </aop:config>
- </beans>
測試類如下:
- public static void main(String[] args) {
- ApplicationContextcontext = new ClassPathXmlApplicationContext("bean3.xml");
- UserServiceuserService = (UserService) context.getBean("userService");
- userService.search();
- }
執(zhí)行測試代碼,結(jié)果如下
com.my.demo.aop.UserServiceImpl的search方法被執(zhí)行了 //before方法執(zhí)行
查詢用戶 //UserServiceImpl中的search方法
執(zhí)行執(zhí)行了
com.my.demo.aop.UserServiceImpl的search方法,返回值:null//after方法執(zhí)行
可以發(fā)現(xiàn)由于實(shí)現(xiàn)了@Before通知@After通知,我們在調(diào)用方法前后,就分別自動(dòng)對before 和afterReturning完成了調(diào)用。