国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Spring】Spring AOP 初識及實現(xiàn)原理解析

這篇具有很好參考價值的文章主要介紹了【Spring】Spring AOP 初識及實現(xiàn)原理解析。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

【Spring】Spring AOP 初識及實現(xiàn)原理解析,JavaEE進(jìn)階,spring,java,后端,java-ee

  • 博主簡介:想進(jìn)大廠的打工人
  • 博主主頁:@xyk:
  • 所屬專欄:?JavaEE進(jìn)階?

目錄

文章目錄

一、初識AOP

1.1 什么是AOP?

1.2 AOP的組成

1.2.1 切面(Aspect)

1.2.2 切點(Pointcut)

1.2.3?連接點(Join Point)

1.2.4 通知(Advice)

1.3 AOP的使用場景

二、Srping AOP 實現(xiàn)

2.1 添加Spring AOP 依賴

2.2 定義切面和切點

2.3 定義通知

三、Spring AOP 實現(xiàn)原理

3.1 什么是動態(tài)代理?

3.2 JDK 動態(tài)代理實現(xiàn)

3.3?CGLIB 動態(tài)代理實現(xiàn)

3.4?JDK 和 CGLIB 實現(xiàn)的區(qū)別


一、初識AOP

1.1 什么是AOP?

AOP(Aspect Oriented Programming):面向切面編程,它是?種思想,它是對某?類事情的
集中處理。
在我們想要對某一件事情進(jìn)行集中處理,就可以使用到AOP,它提供一種將程序中的橫切關(guān)注點模塊化的方式。在 AOP 中,我們將這些橫切關(guān)注點稱為“切面”,它們獨立于業(yè)務(wù)邏輯模塊,但是可以在程序運行的不同階段被織入到業(yè)務(wù)邏輯中。

簡單來說,AOP 就是對某一件事進(jìn)行集中處理的思想方式~

1.2 AOP的組成

1.2.1 切面(Aspect)

切?(Aspect)由切點(Pointcut)和通知(Advice)組成,它既包含了橫切邏輯的定義,也包
括了連接點的定義。相當(dāng)于處理某方面具體問題的一個類,包含多個方法,而這些方法就是切點和通知。

1.2.2 切點(Pointcut)

Pointcut 的作?就是提供?組規(guī)則來匹配連接點(Join Point),給滿足規(guī)則的連接點添加通知(Advice),可以理解為用來進(jìn)行主動攔截的規(guī)則(配置)

1.2.3?連接點(Join Point)

應(yīng)?執(zhí)?過程中能夠插?切?的?個點,連接點可以理解為可能會觸發(fā)AOP規(guī)則的所有點。(所有請求)

1.2.4 通知(Advice)

在AOP術(shù)語中,切面的工作被稱之為通知。通知是切面在連接點上執(zhí)行的動作。它定義了在何時(例如在方法調(diào)用之前或之后)以及如何(例如打印日志或進(jìn)行性能監(jiān)控)應(yīng)用切面的行為。即,程序中被攔截請求觸發(fā)的具體動作。

Spring 切?類中,可以在方法上使?以下注解,會設(shè)置?法為通知方法,在滿?條件后會通知本
?法進(jìn)?調(diào)?:

  1. 前置通知使? @Before:通知?法會在?標(biāo)?法調(diào)?之前執(zhí)行。
  2. 后置通知使? @After:通知?法會在?標(biāo)?法返回或者拋出異常后調(diào)?。
  3. 返回之后通知使? @AfterReturning:通知?法會在?標(biāo)?法返回后調(diào)?。
  4. 拋異常后通知使? @AfterThrowing:通知?法會在?標(biāo)?法拋出異常后調(diào)?。
  5. 環(huán)繞通知使? @Around:通知包裹了被通知的?法,在被通知的?法通知之前和調(diào)?之后執(zhí)行?定義的行為。

1.3 AOP的使用場景

在做任何一個系統(tǒng)都需要登錄功能,那么幾乎想要使用這個系統(tǒng)都需要我們進(jìn)行驗證用戶登錄狀態(tài),我們之前的處理?式是每個 Controller 都要寫?遍?戶登錄驗證,然?當(dāng)你的功能越來越多,那么你要寫的登錄驗證也越來越多,?這些?法?是相同的,這么多的?法就會代碼修改和維護(hù)的成本。對于這種功能統(tǒng)?,且使?的地?較多的功能,就可以考慮 AOP來統(tǒng)?處理了。

【Spring】Spring AOP 初識及實現(xiàn)原理解析,JavaEE進(jìn)階,spring,java,后端,java-ee

【Spring】Spring AOP 初識及實現(xiàn)原理解析,JavaEE進(jìn)階,spring,java,后端,java-ee?

除了統(tǒng)一登錄判斷外,使用AOP還可以實現(xiàn):

  • 用戶登錄驗證
  • 統(tǒng)??志記錄
  • 統(tǒng)??法執(zhí)?時間統(tǒng)計
  • 統(tǒng)?的返回格式設(shè)置
  • 統(tǒng)?的異常處理
  • 事務(wù)的開啟和提交等

二、Srping AOP 實現(xiàn)

Spring AOP 的實現(xiàn)步驟如下:

  1. 添加 Spring AOP 框架?持
  2. 定義切?和切點:(1)創(chuàng)建切面類(2)配置攔截規(guī)則
  3. 定義通知

2.1 添加Spring AOP 依賴

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-bo
ot-starter-aop -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

2.2 定義切面和切點

使用?@Aspect?注解表明當(dāng)前類為一個切面,而在切點中,我們要定義攔截的規(guī)則,具體實現(xiàn)如下:

@Component // 隨著框架的啟動而啟動
@Aspect // 告訴框架我是一個切面類
public class UserAspect {

    // 定義切點(配置攔截規(guī)則)
    @Pointcut("execution(* com.example.demo.controller.UserController.*(..))")
    public void pointcut(){

    }
}

在上述實現(xiàn)代碼中,pointcut 為一個空方法,只是起到一個“標(biāo)識”的作用,標(biāo)識下面的通知方法具體指的是哪個切點,切點可以有多個。

切點表達(dá)式由切點函數(shù)組成,其中?execution()?是最常?的切點函數(shù),?來匹配?法,語法為:

execution(<修飾符><返回類型><包.類.?法(參數(shù))><異常>)
修飾符和異??梢允÷?/span>

常見的切點表達(dá)式的示例:

  • 匹配特定類的所有方法:
  • execution(* com.example.MyClass.*(..)):匹配 com.example.MyClass 類中的所有方法。
  • 匹配特定包下的所有方法:
  • execution(* com.example.*.*(..)):匹配 com.example 包及其子包下的所有方法。
  • 匹配特定方法名的方法:
  • execution(* com.example.MyClass.myMethod(..)):匹配 com.example.MyClass 類中名為 myMethod 的方法。
  • 匹配特定方法參數(shù)類型的方法:
  • execution(* com.example.MyClass.myMethod(String, int)):匹配 com.example.MyClass 類中具有一個 String 參數(shù)和一個 int 參數(shù)的 myMethod 方法。
  • 匹配特定返回類型的方法:
  • execution(String com.example.MyClass.myMethod(..)):匹配 com.example.MyClass 類中返回類型為 String 的 myMethod 方法。
package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/hi")
    public String sayHi(String name){
        System.out.println("執(zhí)行了Hi");
        return "Hi," + name;
    }

    @RequestMapping("/hello")
    public String sayHello(){
        System.out.println("執(zhí)行了Hello");
        return "Hello,world";
    }
}

2.3 定義通知

通知定義的是被攔截方法具體要執(zhí)行的業(yè)務(wù)。我們上面列出了可以使用哪些通知~這里舉出例子

package com.example.demo.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component // 隨著框架的啟動而啟動
@Aspect // 告訴框架我是一個切面類
public class UserAspect {


    // 定義切點(配置攔截規(guī)則)
    @Pointcut("execution(* com.example.demo.controller.UserController.*(..))")
    public void pointcut(){

    }

    @Before("pointcut()")
    public void beforeAdvice(){
        System.out.println("執(zhí)行了前置通知~");
    }

    @After("pointcut()")
    public void AfterAdvice(){
        System.out.println("執(zhí)行了后置通知~");
    }

    /**
     * 環(huán)繞通知
     * @param joinPoint
     * @return
     */
    @Around("pointcut()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint){
        System.out.println("進(jìn)入了環(huán)繞通知~");
        Object obj = null;
        try {
        // 執(zhí)?攔截?法
            obj = joinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("退出了環(huán)繞通知~");
        return obj;
    }

}

環(huán)繞通知是在前置通知之前和后置通知之后運行的~

【Spring】Spring AOP 初識及實現(xiàn)原理解析,JavaEE進(jìn)階,spring,java,后端,java-ee ??

三、Spring AOP 實現(xiàn)原理

Spring AOP 是通過動態(tài)代理的?式,在運?期將 AOP 代碼織?到程序中的,它的實現(xiàn)?式有兩種:JDK Proxy?和?CGLIB。因此,Spring 對 AOP 的支持局限于方法級別的攔截。

  1. 默認(rèn)情況下,實現(xiàn)了接?的類,使? AOP 會基于 JDK ?成代理類
  2. 沒有實現(xiàn)接?的類,會基于 CGLIB ?成代理類
    ?

3.1 什么是動態(tài)代理?

動態(tài)代理(Dynamic Proxy)是一種設(shè)計模式,它允許 在運行時創(chuàng)建代理對象,并將方法調(diào)用轉(zhuǎn)發(fā)給實際的對象。 動態(tài)代理可以用于實現(xiàn)橫切關(guān)注點(如日志記錄、性能監(jiān)控、事務(wù)管理等)的功能,而無需修改原始對象的代碼。

在Java中,動態(tài)代理通常使用 java.lang.reflect.Proxy 類和 java.lang.reflect.InvocationHandler 接口來實現(xiàn)。

調(diào)用者在調(diào)用方法時,會先轉(zhuǎn)發(fā)給代理類創(chuàng)建的代理對象,隨后再由代理對象轉(zhuǎn)發(fā)給目標(biāo)對象。

【Spring】Spring AOP 初識及實現(xiàn)原理解析,JavaEE進(jìn)階,spring,java,后端,java-ee

以下是使用動態(tài)代理的一般步驟:

  1. 創(chuàng)建一個實現(xiàn)InvocationHandler接口的類,該類將作為代理對象的調(diào)用處理程序。在InvocationHandler接口的invoke方法中,可以定義在方法調(diào)用前后執(zhí)行的邏輯。
  2. 使用Proxy類的newProxyInstance方法創(chuàng)建代理對象。該方法接受三個參數(shù):類加載器、代理接口數(shù)組和調(diào)用處理程序。它將返回一個實現(xiàn)指定接口的代理對象。
  3. 使用代理對象調(diào)用方法。當(dāng)調(diào)用代理對象的方法時,實際上會調(diào)用調(diào)用處理程序的invoke方法,并將方法調(diào)用轉(zhuǎn)發(fā)給實際的對象。
    ?

3.2 JDK 動態(tài)代理實現(xiàn)

先通過實現(xiàn) InvocationHandler 接?創(chuàng)建?法調(diào)?處理器,再通過 Proxy 來創(chuàng)建代理類。文章來源地址http://www.zghlxwxcb.cn/news/detail-639029.html

import org.example.demo.service.AliPayService;
import org.example.demo.service.PayService;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
//動態(tài)代理:使?JDK提供的api(InvocationHandler、Proxy實現(xiàn)),此種?式實現(xiàn),要求被代理類必須實現(xiàn)接?
public class PayServiceJDKInvocationHandler implements InvocationHandler {
    //?標(biāo)對象即就是被代理對象
    private Object target;
    public PayServiceJDKInvocationHandler( Object target) {
        this.target = target;
    }
    //proxy代理對象
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    //1.安全檢查
        System.out.println("安全檢查");
    //2.記錄?志
        System.out.println("記錄?志");
    //3.時間統(tǒng)計開始
        System.out.println("記錄開始時間");
    //通過反射調(diào)?被代理類的?法
        Object retVal = method.invoke(target, args);
    //4.時間統(tǒng)計結(jié)束
        System.out.println("記錄結(jié)束時間");
        return retVal;
    }
    public static void main(String[] args) {
        PayService target= new AliPayService();
    //?法調(diào)?處理器
        InvocationHandler handler =
                new PayServiceJDKInvocationHandler(target);
    //創(chuàng)建?個代理類:通過被代理類、被代理實現(xiàn)的接?、?法調(diào)?處理器來創(chuàng)建
        PayService proxy = (PayService) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                new Class[]{PayService.class},
                handler
        );
        proxy.pay();
    }
}

3.3?CGLIB 動態(tài)代理實現(xiàn)

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.example.demo.service.AliPayService;
import org.example.demo.service.PayService;
import java.lang.reflect.Method;

public class PayServiceCGLIBInterceptor implements MethodInterceptor {
    //被代理對象
    private Object target;
    public PayServiceCGLIBInterceptor(Object target){
        this.target = target;
    }
    @Override
    public Object intercept(Object o, Method method, Object[] args, Method
            Proxy methodProxy) throws Throwable {
//1.安全檢查
        System.out.println("安全檢查");
//2.記錄?志
        System.out.println("記錄?志");
//3.時間統(tǒng)計開始
        System.out.println("記錄開始時間");
//通過cglib的代理?法調(diào)?
        Object retVal = methodProxy.invoke(target, args);
//4.時間統(tǒng)計結(jié)束
        System.out.println("記錄結(jié)束時間");
        return retVal;
    }
    public static void main(String[] args) {
        PayService target= new AliPayService();
        PayService proxy= (PayService) Enhancer.create(target.getClass(),n
                ew PayServiceCGLIBInterceptor(target));
        proxy.pay();
    }
}

3.4?JDK 和 CGLIB 實現(xiàn)的區(qū)別

  1. JDK 實現(xiàn),要求被代理類必須實現(xiàn)接口, 之后是通過 InvocationHandler 及 Proxy,在運?時動態(tài)的在內(nèi)存中?成了代理類對象,該代理對象是通過實現(xiàn)同樣的接?實現(xiàn)(類似靜態(tài)代理接?實現(xiàn)的?式),只是該代理類是在運?期時,動態(tài)的織?統(tǒng)?的業(yè)務(wù)邏輯字節(jié)碼來完成。
  2. CGLIB 實現(xiàn),被代理類可以不實現(xiàn)接口, 是通過繼承被代理類,在運?時動態(tài)的?成代理類對象。

到了這里,關(guān)于【Spring】Spring AOP 初識及實現(xiàn)原理解析的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 【微服務(wù)】Spring Aop原理深入解析

    目錄 一、前言 二、aop概述 2.1 什么是AOP 2.2 AOP中的一些概念 2.2.1 aop通知類型

    2024年02月04日
    瀏覽(25)
  • Spring AOP(AOP概念、組成、Spring AOP實現(xiàn)及實現(xiàn)原理)

    Spring AOP(AOP概念、組成、Spring AOP實現(xiàn)及實現(xiàn)原理)

    學(xué)習(xí) Spring AOP 之前,先要了解 AOP 是什么 AOP(Aspect Oriented Programming):面向切面編程,它和 OOP(面向?qū)ο缶幊蹋╊愃啤?它是一種思想, 是對某一類事情的集中處理。 比如用戶登錄權(quán)限的效驗,在學(xué)習(xí) AOP 之前,在需要判斷用戶登錄的頁面,都要各自實現(xiàn)或調(diào)用用戶驗證的方

    2024年02月02日
    瀏覽(23)
  • 【Spring】Spring AOP入門及實現(xiàn)原理剖析

    【Spring】Spring AOP入門及實現(xiàn)原理剖析

    AOP (Aspect-Oriented Programming) 是一種編程范式,它提供一種將程序中的橫切關(guān)注點模塊化的方式。橫切關(guān)注點可以是日志、事務(wù)、安全等,它們不屬于業(yè)務(wù)邏輯,但是又必須要與業(yè)務(wù)邏輯緊密耦合在一起。在 AOP 中,我們將這些橫切關(guān)注點稱為“切面”,它們獨立于業(yè)務(wù)邏輯模塊

    2024年02月17日
    瀏覽(59)
  • Spring AOP實現(xiàn)原理

    Spring AOP實現(xiàn)原理

    從入口? org.springframework.context.support.AbstractApplicationContext#refresh ?開始看 找到Bean的創(chuàng)建方法進(jìn)入: 再進(jìn)入詳細(xì)方法: ?找到getBean(beanName): 找到doGetBean(xxx,xxx,xxx,xxx); ? 找到實際的Bean創(chuàng)建方法createBean(beanName, mdb, args);可以非常明顯的看到,Bean就是通過Proxy的方式獲取的。 ? 繼續(xù)

    2023年04月25日
    瀏覽(22)
  • Spring AOP(AOP概念,組成成分,實現(xiàn),原理)

    Spring AOP(AOP概念,組成成分,實現(xiàn),原理)

    目錄 1. 什么是Spring AOP? 2. 為什么要用AOP? 3. AOP該怎么學(xué)習(xí)? 3.1 AOP的組成 (1)切面(Aspect) (2)連接點(join point) (3)切點(Pointcut) (4)通知(Advice) ?4. Spring AOP實現(xiàn) 4.1 添加 AOP 框架支持 ?編輯 ?4.2 定義切面 4.3 定義切點 4.4 定義通知 4.5 切點表達(dá)式說明 AspectJ

    2024年02月13日
    瀏覽(49)
  • Spring AOP的原理與實現(xiàn)

    Spring AOP的原理與實現(xiàn)

    前言: 博主在最近的幾次面試中,大中小廠都問到了Spring的AOP思想相關(guān)問題,這塊知識確實是面試中的重點內(nèi)容,因此結(jié)合所看的書籍,在這篇文章中總結(jié)下。該專欄比較適合剛?cè)肟覬ava的小白以及準(zhǔn)備秋招的大佬閱讀,感謝大佬的關(guān)注。 如果文章有什么需要改進(jìn)的地方歡迎

    2024年02月13日
    瀏覽(27)
  • javaee spring 測試aop 切面

    javaee spring 測試aop 切面

    spring配置文件

    2024年02月09日
    瀏覽(22)
  • Spring AOP 實現(xiàn)原理和使用場景

    Spring AOP 是通過在目標(biāo)方法執(zhí)行前、執(zhí)行后、拋出異常時等切入點執(zhí)行切面代碼的一種機(jī)制。其實現(xiàn)原理是使用動態(tài)代理技術(shù),在方法運行時動態(tài)生成代理對象,然后插入切面代碼。當(dāng)執(zhí)行目標(biāo)方法時,由動態(tài)代理對象攔截方法并在適當(dāng)?shù)臅r間點執(zhí)行切面代碼,然后再調(diào)用實

    2024年02月05日
    瀏覽(45)
  • 【Spring AOP學(xué)習(xí)】AOP的組成 && SpringAOP的實現(xiàn)和實現(xiàn)原理

    【Spring AOP學(xué)習(xí)】AOP的組成 && SpringAOP的實現(xiàn)和實現(xiàn)原理

    目錄 一、認(rèn)識SpringAOP 1、AOP是什么? 2、AOP的功能 3、AOP的組成(重要) 二、SpringAOP的實現(xiàn) ??1、添加Spring AOP框架支持 ??2、定義切面和切點 ???3、定義通知 3.1 完成代碼實現(xiàn) 3.2 具體通知分析 ??4、小練習(xí):使用AOP統(tǒng)計UserController每個方法的執(zhí)行時間。 ?三、SpringAOP的實現(xiàn)

    2024年02月15日
    瀏覽(21)
  • 【JavaEE進(jìn)階】 利用Spring簡單實現(xiàn)加法計算器和用戶登錄

    【JavaEE進(jìn)階】 利用Spring簡單實現(xiàn)加法計算器和用戶登錄

    本篇博客主要內(nèi)容: 理解前后端交互過程 接?傳參,數(shù)據(jù)返回,以及??展? 需求:輸?兩個整數(shù),點擊\\\"點擊相加\\\"按鈕,顯?計算結(jié)果 效果展示如下: 具體實現(xiàn)步驟,博主大致分為以下幾步: 準(zhǔn)備工作 約定前后端交互接? 后端服務(wù)器代碼的書寫 創(chuàng)建SpringBoot項?: 引?Spring Web依

    2024年01月17日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包