欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何使用SpringAOP進(jìn)行測試

本篇內(nèi)容主要講解“如何使用Spring AOP進(jìn)行測試”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“如何使用Spring AOP進(jìn)行測試”吧!

創(chuàng)新互聯(lián)建站憑借專業(yè)的設(shè)計團(tuán)隊扎實(shí)的技術(shù)支持、優(yōu)質(zhì)高效的服務(wù)意識和豐厚的資源優(yōu)勢,提供專業(yè)的網(wǎng)站策劃、網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)站優(yōu)化、軟件開發(fā)、網(wǎng)站改版等服務(wù),在成都10余年的網(wǎng)站建設(shè)設(shè)計經(jīng)驗,為成都上千中小型企業(yè)策劃設(shè)計了網(wǎng)站。

AOP解析

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

AOP (Aspect OrientProgramming),直譯過來就是 面向切面編程。AOP  是一種編程思想,是面向?qū)ο缶幊?OOP)的一種補(bǔ)充。面向?qū)ο缶幊虒⒊绦虺橄蟪筛鱾€層次的對象,而面向切面編程是將程序抽象成各個切面。從《Spring實(shí)戰(zhàn)(第4版)》圖書中扒了一張圖:

如何使用Spring AOP進(jìn)行測試

從該圖可以很形象地看出,所謂切面,相當(dāng)于應(yīng)用對象間的橫切點(diǎn),我們可以將其單獨(dú)抽象為單獨(dú)的模塊。

Spring提供了面向切面編程的豐富支持,是通過動態(tài)代理實(shí)現(xiàn)的。允許通過分離應(yīng)用的業(yè)務(wù)邏輯與系統(tǒng)級服務(wù)(例如:審計(auditing)和事務(wù)(transaction)管理)進(jìn)行內(nèi)聚性的開發(fā)。應(yīng)用對象只實(shí)現(xiàn)它們應(yīng)該做的——完成業(yè)務(wù)邏輯——僅此而已。它們并不負(fù)責(zé)(甚至是意識到)其它系統(tǒng)級別的關(guān)注點(diǎn),例如:日志或事務(wù)支持。

AOP 要達(dá)到的效果是,保證開發(fā)者在不修改源代碼的前提下,去為系統(tǒng)中的業(yè)務(wù)組件添加某種通用功能。

AOP基本運(yùn)行流程如下圖所示:

如何使用Spring AOP進(jìn)行測試

AOP 領(lǐng)域中的特性術(shù)語:

  • 橫切關(guān)注點(diǎn):跨越應(yīng)用程序多個模塊的方法或功能。即是與我們業(yè)務(wù)邏輯無關(guān)的,但是我們需要關(guān)注的部分,就是橫切關(guān)注點(diǎn)。如日志 , 安全 , 緩存 ,  事務(wù)等等....

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

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

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

  • 代理(Proxy):向目標(biāo)對象應(yīng)用通知之后創(chuàng)建的對象。

  • 連接點(diǎn)(JointPoint):表示應(yīng)用執(zhí)行過程中能夠插入切面的一個點(diǎn),這個點(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)建一個被增強(qiáng)的對象,這個過程就是織入。

Advice通知

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

如何使用Spring AOP進(jìn)行測試

各個通知的執(zhí)行順序如下圖所示:

如何使用Spring AOP進(jìn)行測試

實(shí)例編碼

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

如何使用Spring AOP進(jìn)行測試

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

實(shí)現(xiàn)方法2:通過aop來實(shí)現(xiàn)

首先,mvn中添加配置


如何使用Spring AOP進(jìn)行測試

實(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)用方法前后,就分別自動對before  和afterReturning完成了調(diào)用。

到此,相信大家對“如何使用Spring AOP進(jìn)行測試”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

標(biāo)題名稱:如何使用SpringAOP進(jìn)行測試
文章鏈接:http://www.chinadenli.net/article24/joiije.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號ChatGPT網(wǎng)站收錄企業(yè)網(wǎng)站制作響應(yīng)式網(wǎng)站網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)