AOP面向切麵編程簡單介紹

来源:https://www.cnblogs.com/bingshen/archive/2022/08/26/16629592.html
-Advertisement-
Play Games

AOP面向切麵編程 什麼是AOP AOP (Aspect Oriented Programming)意為:面向切麵編程,通過預編譯方式和運行期動態代理實現程式功能的統一維護的一種技術。AOP是OOP的延續,是軟體開發中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生範型。利用 ...


AOP面向切麵編程

什麼是AOP

AOP (Aspect Oriented Programming)意為:面向切麵編程,通過預編譯方式和運行期動態代理實現程式功能的統一維護的一種技術。AOP是OOP的延續,是軟體開發中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生範型。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程式的可重用性,同時提高了開發的效率。

OOP (Object Oriented Programming) 面向對象編程

AOP (Aspect Oritented Programming) 面向切麵編程

OOP 到AOP 不是替換的關係,而是一種擴展,使用了AOP後,OOP還是會繼續使用

Aop在Spring中的作用

提供聲明式事務;允許用戶自定義切麵

  • 橫切關註點:跨越應用程式多個模塊的方法或功能。即是,與我們業務邏輯無關的,但是我們需要關註的部分,就是橫切關註點。如日誌,安全,緩存,事務等等.....
  • 切麵(ASPECT)︰橫切關註點被模塊化的特殊對象。即,它是一個類。
  • 通知(Advice) :切麵必須要完成的工作。即,它是類中的一個方法。·
  • 目標(Target):被通知對象。
  • 代理(Proxy) ︰向目標對象應用通知之後創建的對象。
  • 切入點(PointCut) :切麵通知執行的“地點""的定義。
  • 連接點(JointPoint) : 與切入點匹配的執行點。

AOP 主要就是在不改變原本代碼的前提下,新增功能上去不影響原本的功能。

AOP在Spring中是非常重要的一個功能,可以理解為一個業務就是一條線,當使用一把刀在這條線的指定位置砍下去,添加新的功能到斷開處,最後在進行織入,最後連起來成為了一條新的線,新的功能就可以實現。

使用Spring實現Aop

添加依賴

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.3.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.9.6</version>
    <scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.3.18</version>
</dependency>

註解實現

修改配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xs">
    <!--註解掃描 預設開啟註解支持-->
    <context:component-scan base-package="com.bing"/>
    <!--
    proxy-target-class="true" 如果是true 就是cglib代理 如果是false就是jdk 預設是false
    -->
    <aop:aspectj-autoproxy />

</beans>

添加一個切麵類

package com.bing.aspect;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @Author IBing
 * @Date 2022/8/26 22:01
 * @Version 1.0
 */
@Aspect  //表示這是一個 切麵類
@Component  //將對象放入spring容器
public class UserAspect {
	//開啟日誌
    private Logger logger = Logger.getLogger(UserAspect.class);

    /**
     * 目標方法執行之前執行
     * @param joinPoint
     */
    @Before("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
    public void before(JoinPoint joinPoint){
        logger.debug("before");
        logger.debug("攔截的目標對象"+joinPoint.getTarget());
        logger.debug("攔截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
        logger.debug("攔截的參數"+joinPoint.getArgs());
        logger.debug("攔截的位置"+joinPoint.getStaticPart());
        logger.debug("攔截的代理對象"+joinPoint.getStaticPart());
    }

    /**
     * 無論是否拋出異常都會執行,相當於finally
     * @param joinPoint
     */
    @After("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
    public void after(JoinPoint joinPoint){
        logger.debug("after");
        logger.debug("攔截的目標對象"+joinPoint.getTarget());
        logger.debug("攔截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
        logger.debug("攔截的參數"+joinPoint.getArgs());
        logger.debug("攔截的位置"+joinPoint.getStaticPart());
        logger.debug("攔截的代理對象"+joinPoint.getStaticPart());
    }

    /**
     * 相當於try
     * @param joinPoint
     */
    @AfterReturning("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
    public void afterReturning(JoinPoint joinPoint){
        logger.debug("afterReturning");
        logger.debug("攔截的目標對象"+joinPoint.getTarget());
        logger.debug("攔截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
        logger.debug("攔截的參數"+joinPoint.getArgs());
        logger.debug("攔截的位置"+joinPoint.getStaticPart());
        logger.debug("攔截的代理對象"+joinPoint.getStaticPart());
    }

    /**
     * 相當於catch
     * @param joinPoint
     * @param e
     */
    @AfterThrowing(value="execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))",throwing ="e" )
    public void afterThrowing(JoinPoint joinPoint,RuntimeException e){
        logger.debug("afterThrowing");
        logger.debug("攔截的目標對象"+joinPoint.getTarget());
        logger.debug("拋出的異常為",e);
    }

    /**
     * 環繞執行
     * @param pjd
     * @return
     * @throws Throwable
     */
    @Around("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
    public Object around(ProceedingJoinPoint pjd) throws Throwable {
        logger.debug("around之前");
        Object proceed= pjd.proceed();
        logger.debug("around之後");
        return proceed;
    }
}

測試

@Test
public void test( ){
    //創建spring的容器並初始化
    ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("beans.xml");
    String[] beanNameForType = classPathXmlApplicationContext.getBeanNamesForType(UserController.class);
    System.out.println(Arrays.toString(beanNameForType));
    UserController bean = (UserController) classPathXmlApplicationContext.getBean("userController");
    bean.login("aa","123"); //這是 UserController 里寫好的login方法 
    classPathXmlApplicationContext.close();
    }

輸出結果

因為沒有異常,所以 **afterThrowing **方法沒有執行

上面每個方法的切入點都一樣,代碼重覆,我們可以定義切入點,

//定義切入點
@Pointcut("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
private void pointCut(){}

然後在切麵類的方法上使用切入點方法就行,這樣就減少了代碼重覆

/**
 * 目標方法執行之前執行
 * @param joinPoint
 */
@Before("pointCut()")
public void before(JoinPoint joinPoint){
    logger.debug("before");
    logger.debug("攔截的目標對象"+joinPoint.getTarget());
    logger.debug("攔截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
    logger.debug("攔截的參數"+joinPoint.getArgs());
    logger.debug("攔截的位置"+joinPoint.getStaticPart());
    logger.debug("攔截的代理對象"+joinPoint.getStaticPart());
}

下麵是一些切入點語法

execution()   切入點
直接精準到一個方法上面去
execution( public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
任意許可權修飾符
execution(  com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
無返回類型
execution( void com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
有返回類型
execution( !void com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
任意返回類型
execution( * com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
任意參數
execution( * com.bing.service.impl.UserServiceImpl.login(..))
類中的任意方法
execution( * com.bing.service.impl.UserServiceImpl.*(..))
類中以指定內容開頭的方法
execution( * com.bing.service.impl.UserServiceImpl.select*(..))
包中的任意類的任意方法不包含子包下麵的類
execution( * com.bing.service.impl.*.*(..))
包中及其下的任意類的任意方法
execution( * com.bing.service..*.*(..))

名詞解釋

pointcut 切入點 定義切入的連接點, 一般對應的就是表達式
aspect 切麵 擁有具體功能的一個類
advice 通知 切麵的具體實現 對應的就是切麵類中的方法
joinpoint 連接點 程式運行中可以插入切麵的地方 在spring中只能是方法 比如login方法
target 目標對象 切入的對象 這個對象包含了業務代碼的具體實現 比如:UserServiceImpl類的對象
proxy 代理對象 目標對象應用了通知以後創建的一個新的對象,這個對象中包含了原本的業務實現和擴展實現
weaving 織入 將通知應用到目標對象後創建代理對象的過程

XML實現AOP

首先在配置文件中進行AOP相應的配置

<beans xmlns="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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--開啟註解支持,,讓項目支持spring的註解-->
<!--    <context:annotation-config></context:annotation-config>-->
    <!--註解掃描 預設開啟註解支持-->
    <context:component-scan base-package="com.bing">
        <!--設置需要掃描的註解-->
        <!--        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>-->
        <!--設置不掃描的註解-->
        <!--        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component"/>-->
    </context:component-scan>
<!--
    打開動態代理
    proxy-target-class="true" 就是cglib代理 ,為false則會使用jdk代理實現,預設為false
 -->
    <aop:aspectj-autoproxy proxy-target-class="false"/>
<!--    下麵這些就是AOP的XML實現方式-->
    <bean id="userAspect" class="com.bing.aspect.UserAspect"/>
    <aop:config>
        <aop:aspect ref="userAspect">
            <!-- 定義切麵-->
            <aop:pointcut id="pc" expression="execution(* com.bing.service.impl.UserServiceImpl.*(..))"/>
            <aop:before method="before" pointcut-ref="pc"/>
            <aop:after-returning method="afterReturning" pointcut-ref="pc"/>
            <aop:after-throwing method="afterThrowing" pointcut-ref="pc" throwing="e"/>
            <aop:after method="after" pointcut-ref="pc"/>
<!--            如果不想使用已經定義的切入點 pc,也可以使用pointcut="execution()"來自己定義,這裡演示就使用相同的切入點-->
            <aop:around method="around" pointcut="execution(* com.bing.service.impl.UserServiceImpl.*(..))"/>
        </aop:aspect>
    </aop:config>
</beans>

切麵類

package com.bing.aspect;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @Author IBing
 * @Date 2022/8/26 22:01
 * @Version 1.0
 */
//@Aspect  //表示這是一個 切麵類
@Component  //將對象放入spring容器
public class UserAspect {

    private Logger logger = Logger.getLogger(UserAspect.class);

    /**
     * 目標方法執行之前執行
     * @param joinPoint
     */
    public void before(JoinPoint joinPoint){
        logger.debug("before");
        logger.debug("攔截的目標對象"+joinPoint.getTarget());
        logger.debug("攔截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
        logger.debug("攔截的參數"+joinPoint.getArgs());
        logger.debug("攔截的位置"+joinPoint.getStaticPart());
        logger.debug("攔截的代理對象"+joinPoint.getStaticPart());
    }

    /**
     * 無論是否拋出異常都會執行,相當於finally
     * @param joinPoint
     */
    public void after(JoinPoint joinPoint){
        logger.debug("after");
        logger.debug("攔截的目標對象"+joinPoint.getTarget());
        logger.debug("攔截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
        logger.debug("攔截的參數"+joinPoint.getArgs());
        logger.debug("攔截的位置"+joinPoint.getStaticPart());
        logger.debug("攔截的代理對象"+joinPoint.getStaticPart());
    }

    /**
     * 相當於try
     * @param joinPoint
     */
    public void afterReturning(JoinPoint joinPoint){
        logger.debug("afterReturning");
        logger.debug("攔截的目標對象"+joinPoint.getTarget());
        logger.debug("攔截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
        logger.debug("攔截的參數"+joinPoint.getArgs());
        logger.debug("攔截的位置"+joinPoint.getStaticPart());
        logger.debug("攔截的代理對象"+joinPoint.getStaticPart());
    }

    /**
     * 相當於catch
     * @param joinPoint
     * @param e
     */
    public void afterThrowing(JoinPoint joinPoint,RuntimeException e){
        logger.debug("afterThrowing");
        logger.debug("攔截的目標對象"+joinPoint.getTarget());
        logger.debug("拋出的異常為",e);
    }

    /**
     * 環繞執行
     * @param pjd
     * @return
     * @throws Throwable
     */
    public Object around(ProceedingJoinPoint pjd) throws Throwable {
        logger.debug("around之前");
        Object proceed= pjd.proceed();
        logger.debug("around之後");
        return proceed;
    }
}

運行結果


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 千分位格式化 在項目中經常碰到關於貨幣金額的頁面顯示,為了讓金額的顯示更為人性化與規範化,需要加入貨幣格式化策略。也就是所謂的數字千分位格式化。 例如 123456789 => 123,456,789 、 123456789.123 => 123,456,789.123 const formatMo ...
  • 根據`業務功能`進行模塊化一直以來都是後端的普遍做法,而Web前端則通常都是按照UI界面的視圖區塊`View`來進行模塊化,這樣的模塊實際上只是`Component組件`,不具備獨立自治的能力。究其原因我想是因為在早期Web1.0的時代,前端的職能就是僅僅作為後端API數據的一個Render渲染器,... ...
  • 首先MDN官網中有一句寫道: assign()語法: Object.assign(target, ...sources) target:目標對象,接收源對象屬性的對象,也是修改後的返回值。sources:源對象,包含將被合併的屬性。下麵來看例子: let s = { a: 1 } let targe ...
  • Sass提供了許多內置模塊,其中包含有用的函數(以及mixin)。這些模塊可以像任何用戶定義的樣式表一樣使用@use規則載入,它們的函數可以像任何其他模塊成員一樣調用。所有內置模塊URL都以sass開頭:表示它們是sass本身的一部分。 常見函數簡介,更多函數列表可看:https://sass-la ...
  • 引言 領域驅動設計並不是新的架構設計理論,從Eric Evans提出至今已經有十多年曆史。由於微服務架構的興起,DDD常用於指導微服務邊界劃分,並重新廣泛進入軟體研發大眾的視野。DDD的理念及應用普及在國外相對成熟,在國內尚處於初期發展階段。國內的很多社區以及企業組織內部近幾年對於DDD的探討和應用 ...
  • 微服務說起來高大尚,實際擼一遍來試試看,用現在比較常用的微服務框架,Consul和Ocelote做一個Demo,一起來試試吧! 說在前面的話 準備好環境,拉取源代碼,按照每個章節中的【實踐操作】進行操作,即可搭建起來,已經測試通過。 不想把篇幅拉太長,且此文實踐為主,如果對微服務沒有理論基礎,建議先 ...
  • 單例模式是一種創建型設計模式, 讓開發者能夠保證一個類只有一個實例, 並提供一個訪問該實例的全局節點,有助於協調系統整體的行為。 ...
  • 類成員函數指針(member function pointer),是 C++ 語言的一類指針數據類型,用於存儲一個指定類具有給定的形參列表與返回值類型的成員函數的訪問信息。一般我們是不會使用的,都是直接將帶有返回值的函數作為參數或者另存後使用;像函數指針我們一般在“由庫的提供者決定函數調用時機,庫的 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...