溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用Spring AOP進行測試

發布時間:2021-10-15 09:53:04 來源:億速云 閱讀:231 作者:iii 欄目:web開發

本篇內容主要講解“如何使用Spring AOP進行測試”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何使用Spring AOP進行測試”吧!

AOP解析

今天來介紹Spring的另一個核心技術點AOP,AOP的概念不好理解,希望大家仔細閱讀文章并按照文章中的代碼進行練習,屆時一定會有很大的收獲!

AOP (Aspect OrientProgramming),直譯過來就是 面向切面編程。AOP  是一種編程思想,是面向對象編程(OOP)的一種補充。面向對象編程將程序抽象成各個層次的對象,而面向切面編程是將程序抽象成各個切面。從《Spring實戰(第4版)》圖書中扒了一張圖:

如何使用Spring AOP進行測試

從該圖可以很形象地看出,所謂切面,相當于應用對象間的橫切點,我們可以將其單獨抽象為單獨的模塊。

Spring提供了面向切面編程的豐富支持,是通過動態代理實現的。允許通過分離應用的業務邏輯與系統級服務(例如:審計(auditing)和事務(transaction)管理)進行內聚性的開發。應用對象只實現它們應該做的——完成業務邏輯——僅此而已。它們并不負責(甚至是意識到)其它系統級別的關注點,例如:日志或事務支持。

AOP 要達到的效果是,保證開發者在不修改源代碼的前提下,去為系統中的業務組件添加某種通用功能。

AOP基本運行流程如下圖所示:

如何使用Spring AOP進行測試

AOP 領域中的特性術語:

  • 橫切關注點:跨越應用程序多個模塊的方法或功能。即是與我們業務邏輯無關的,但是我們需要關注的部分,就是橫切關注點。如日志 , 安全 , 緩存 ,  事務等等....

  • 切面(ASPECT):橫切關注點被模塊化的特殊對象。即,它是一個類。

  • 通知(Advice):AOP 框架中的增強處理。通知描述了切面何時執行以及如何執行增強處理。它是類中的一個方法。

  • 目標(Target):被通知對象。

  • 代理(Proxy):向目標對象應用通知之后創建的對象。

  • 連接點(JointPoint):表示應用執行過程中能夠插入切面的一個點,這個點可以是方法的調用、異常的拋出。在 Spring AOP  中,連接點總是方法的調用。

  • 切入點(PointCut):可以插入增強處理的連接點。

  • 引入(Introduction):引入允許我們向現有的類添加新的方法或者屬性。

  • 織入(Weaving): 將增強處理添加到目標對象中,并創建一個被增強的對象,這個過程就是織入。

Advice通知

通知(Advice)是切面的一種實現,可以完成簡單織入功能(織入功能就是在這里完成的)。Spring AOP 中有 5 中通知類型,分別如下:

如何使用Spring AOP進行測試

各個通知的執行順序如下圖所示:

如何使用Spring AOP進行測試

實例編碼

需求:在類中添加日志功能,如下圖:

如何使用Spring AOP進行測試

實現方法1:在各個類中添加方法logMsg()。如果類數量少,問題不大,如果有幾百個類需要處理,那么工作量就很大了。

實現方法2:通過aop來實現

首先,mvn中添加配置


如何使用Spring AOP進行測試

實例如下:

創建接口

public interface UserService {        public void add();        public void delete();        public void update();        public void search();     }

創建切入點類

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("查詢用戶");       }

創建類,實現@Before通知

import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class BeforeLog implements MethodBeforeAdvice {        //method : 要執行的目標對象的方法        //objects : 被調用的方法的參數        //Object : 目標對象        public void before(Method method, Object[] objects, Object o) throws Throwable {           System.out.println( o.getClass().getName() + "的" + method.getName() + "方法被執行了");       }     }

創建類,實現@After通知

import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; public class AfterLog implements AfterReturningAdvice {        //returnValue 返回值        //method被調用的方法        //args被調用的方法的對象的參數        //target 被調用的目標對象        public void afterReturning(Object returnValue,Method method, Object[] args, Object target) throws Throwable {           System.out.println("執行了" + 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:表達式匹配要執行的方法-->        <aop:pointcutid="pointcut"expression="execution(* com.my.demo.aop.UserServiceImpl.*(..))"/>        <!--執行環繞; advice-ref執行方法.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(); }

執行測試代碼,結果如下

com.my.demo.aop.UserServiceImpl的search方法被執行了 //before方法執行

查詢用戶 //UserServiceImpl中的search方法

執行執行了

com.my.demo.aop.UserServiceImpl的search方法,返回值:null//after方法執行

可以發現由于實現了@Before通知@After通知,我們在調用方法前后,就分別自動對before  和afterReturning完成了調用。

到此,相信大家對“如何使用Spring AOP進行測試”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女