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