偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

測試同學上手Spring 之AOP最易懂的解析

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

[[390401]]

前面連續(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)建接口

  1. public interface UserService { 
  2.        public void add(); 
  3.        public void delete(); 
  4.        public void update(); 
  5.        public void search(); 
  6.     } 

 創(chuàng)建切入點類

  1. public class UserServiceImpl implements UserService { 
  2.       public void add() { 
  3.           System.out.println("增加用戶"); 
  4.       } 
  5.        public void delete() { 
  6.           System.out.println("刪除用戶"); 
  7.       } 
  8.        public void update() { 
  9.           System.out.println("更新用戶"); 
  10.       } 
  11.        public void search() { 
  12.           System.out.println("查詢用戶"); 
  13.       } 

 創(chuàng)建類,實現(xiàn)@Before通知

  1. import java.lang.reflect.Method; 
  2. import org.springframework.aop.MethodBeforeAdvice; 
  3. public class BeforeLog implements MethodBeforeAdvice { 
  4.        //method : 要執(zhí)行的目標對象的方法 
  5.        //objects : 被調(diào)用的方法的參數(shù) 
  6.        //Object : 目標對象 
  7.        public void before(Method method, Object[] objects, Object o) throws Throwable { 
  8.           System.out.println( o.getClass().getName() + "的" + method.getName() + "方法被執(zhí)行了"); 
  9.       } 
  10.     } 

 創(chuàng)建類,實現(xiàn)@After通知

  1. import java.lang.reflect.Method; 
  2. import org.springframework.aop.AfterReturningAdvice; 
  3. public class AfterLog implements AfterReturningAdvice { 
  4.        //returnValue 返回值 
  5.        //method被調(diào)用的方法 
  6.        //args被調(diào)用的方法的對象的參數(shù) 
  7.        //target 被調(diào)用的目標對象 
  8.        public void afterReturning(Object returnValue,Method method, Object[] args, Object target) throws Throwable { 
  9.           System.out.println("執(zhí)行了" + target.getClass().getName() 
  10.           +"的"+method.getName()+"方法," 
  11.           +"返回值:"+returnValue); 
  12.       } 
  13.     } 

 編輯xml文件

  1. <?xmlversion="1.0"encoding="UTF-8"?> 
  2. <beansxmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xmlns:context="http://www.springframework.org/schema/context" 
  5.     xmlns:aop="http://www.springframework.org/schema/aop" 
  6.     xmlns:p="http://www.springframework.org/schema/p" 
  7.     xmlns:c="http://www.springframework.org/schema/c" 
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans 
  9.        https://www.springframework.org/schema/beans/spring-beans.xsd 
  10.        http://www.springframework.org/schema/context 
  11.        https://www.springframework.org/schema/context/spring-context.xsd 
  12.        http://www.springframework.org/schema/aop 
  13.         http://www.springframework.org/schema/aop/spring-aop.xsd"> 
  14.     <context:annotation-config/> 
  15.  <!--注冊bean--> 
  16.    <beanid="userService"class="com.my.demo.aop.UserServiceImpl"/> 
  17.    <beanid="beforelog"class="com.my.demo.aop.BeforeLog"/> 
  18.    <beanid="afterLog"class="com.my.demo.aop.AfterLog"/> 
  19.    <aop:config> 
  20.        <!--切入點 expression:表達式匹配要執(zhí)行的方法--> 
  21.        <aop:pointcutid="pointcut"expression="execution(* 
  22. com.my.demo.aop.UserServiceImpl.*(..))"/> 
  23.        <!--執(zhí)行環(huán)繞; advice-ref執(zhí)行方法.pointcut-ref切入點--> 
  24.        <aop:advisoradvice-ref="beforelog"pointcut-ref="pointcut"/> 
  25.        <aop:advisoradvice-ref="afterLog"pointcut-ref="pointcut"/> 
  26.    </aop:config> 
  27. </beans> 

 測試類如下:

  1.  public static void main(String[] args) { 
  2.      ApplicationContextcontext = new ClassPathXmlApplicationContext("bean3.xml"); 
  3.       UserServiceuserService = (UserService) context.getBean("userService"); 
  4.       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)用。

 

責任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2021-03-16 08:22:49

SpringIoCAOP

2021-03-23 08:12:13

SpringDIIoC

2021-03-10 09:21:00

Spring開源框架Spring基礎(chǔ)知識

2022-06-07 07:58:45

SpringSpring AOP

2024-04-10 08:59:39

SpringAOP業(yè)務(wù)

2019-04-28 11:06:01

Hbase架構(gòu)程序員

2022-06-08 08:04:28

Springservicerepository

2023-02-04 18:19:39

2021-05-06 18:17:52

SpringAOP理解

2009-06-19 13:28:30

Spring AOPSpring 2.0

2023-02-01 09:15:41

2022-12-07 08:02:43

Spring流程IOC

2009-06-22 10:41:34

Spring.AOP

2024-11-04 16:29:19

2025-02-21 08:00:00

事務(wù)管理SpringBootJava

2022-02-17 13:39:09

AOP接口方式

2021-05-27 08:47:16

C語言C語言程序開發(fā)

2024-03-04 08:47:17

Spring框架AOP

2021-08-21 14:47:04

混沌工程字節(jié)跳動場景化

2009-06-18 14:54:52

Spring AOP
點贊
收藏

51CTO技術(shù)棧公眾號