欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?AOP中三種增強(qiáng)方式的示例詳解

 更新時(shí)間:2022年07月04日 10:22:57   作者:wl_Honest  
AOP?(Aspect?Orient?Programming),直譯過(guò)來(lái)就是?面向切面編程。AOP?是一種編程思想,是面向?qū)ο缶幊蹋∣OP)的一種補(bǔ)充。本文為大家介紹了Spring?AOP中三種增強(qiáng)方式,感興趣的可以了解一下

什么是AOP

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

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

為什么需要AOP

想象下面的場(chǎng)景,開發(fā)中在多個(gè)模塊間有某段重復(fù)的代碼,我們通常是怎么處理的?顯然,沒有人會(huì)靠“復(fù)制粘貼”吧。在傳統(tǒng)的面向過(guò)程編程中,我們也會(huì)將這段代碼,抽象成一個(gè)方法,然后在需要的地方分別調(diào)用這個(gè)方法,這樣當(dāng)這段代碼需要修改時(shí),我們只需要改變這個(gè)方法就可以了。然而需求總是變化的,有一天,新增了一個(gè)需求,需要再多出做修改,我們需要再抽象出一個(gè)方法,然后再在需要的地方分別調(diào)用這個(gè)方法,又或者我們不需要這個(gè)方法了,我們還是得刪除掉每一處調(diào)用該方法的地方。實(shí)際上涉及到多個(gè)地方具有相同的修改的問(wèn)題我們都可以通過(guò) AOP 來(lái)解決。

AOP術(shù)語(yǔ)

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

  1. 通知(Advice): AOP 框架中的增強(qiáng)處理。通知描述了切面何時(shí)執(zhí)行以及如何執(zhí)行增強(qiáng)處理。
  2. 連接點(diǎn)(join point): 連接點(diǎn)表示應(yīng)用執(zhí)行過(guò)程中能夠插入切面的一個(gè)點(diǎn),這個(gè)點(diǎn)可以是方法的調(diào)用、異常的拋出。在 Spring AOP 中,連接點(diǎn)總是方法的調(diào)用。
  3. 切點(diǎn)(PointCut): 可以插入增強(qiáng)處理的連接點(diǎn)。
  4. 切面(Aspect): 切面是通知和切點(diǎn)的結(jié)合。
  5. 引入(Introduction):引入允許我們向現(xiàn)有的類添加新的方法或者屬性。
  6. 織入(Weaving): 將增強(qiáng)處理添加到目標(biāo)對(duì)象中,并創(chuàng)建一個(gè)被增強(qiáng)的對(duì)象,這個(gè)過(guò)程就是織入。

通過(guò)注解聲明5種通知類型

Spring AOP 中有 5 中通知類型,分別如下:

本章中主要以@Before、@After和@Around為例展示AOP的增強(qiáng)方式。

首先引入依賴,這里只放aop的依賴,其它的依賴請(qǐng)根據(jù)自己的實(shí)際情況引入:

<!-- aop -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.1.18.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.1.18.RELEASE</version>
</dependency>

接著新建一個(gè)切面類TesstAspect,并且定義3個(gè)切點(diǎn),就是后面要測(cè)試的3個(gè)切點(diǎn):

package com.wl.standard.aop.aspect;
 
import com.wl.standard.util.JoinPointUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
 
/**
 * @author wl
 * @date 2022/7/2 16:08
 */
@Slf4j
@Component
@Aspect
public class TestAspect {
 
    @Pointcut("execution(public * com.wl.standard.service.TravelRecordService.getAllRecord(..))")
    public void pointCut1(){};
 
    @Pointcut("execution(public * com.wl.standard.service.CityRailService.getTopRail(..))")
    public void pointCut2(){};
 
    @Pointcut("execution(public * com.wl.standard.service.CityGdpService.compareGDP(..))")
    public void pointCut3(){};
 
}

備注:execution():用于匹配方法執(zhí)行的連接點(diǎn),第一個(gè)*表示匹配任意的方法返回值

@Before

先測(cè)試第一個(gè)增強(qiáng)方法,在切點(diǎn)方法之前執(zhí)行,因?yàn)槭呛?jiǎn)單測(cè)試,就只打印一下日志就好了:

package com.wl.standard.aop.aspect;
 
import com.wl.standard.util.JoinPointUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
 
/**
 * @author wl
 * @date 2022/7/2 16:08
 */
@Slf4j
@Component
@Aspect
public class TestAspect {
 
    @Pointcut("execution(public * com.wl.standard.service.TravelRecordService.getAllRecord(..))")
    public void pointCut1(){};
 
    @Pointcut("execution(public * com.wl.standard.service.CityRailService.getTopRail(..))")
    public void pointCut2(){};
 
    @Pointcut("execution(public * com.wl.standard.service.CityGdpService.compareGDP(..))")
    public void pointCut3(){};
 
    @Before("pointCut1()")
    public void doBefore(JoinPoint joinPoint) {
        log.info("當(dāng)前線程: {} 開始執(zhí)行查詢前任務(wù)...", Thread.currentThread().getName());
    }
}

啟動(dòng)項(xiàng)目,進(jìn)入swagger的頁(yè)面調(diào)用接口測(cè)試:

調(diào)用接口后,在控制臺(tái)可以看到日志打印的先后順序,先執(zhí)行的@Before里的增強(qiáng)方法再執(zhí)行的service里的方法:

@After

接著測(cè)試@After,為了更好的展示增強(qiáng)方式,這次利用JoinPoint獲取參數(shù)。

說(shuō)明:Joinpoint是AOP的連接點(diǎn)。一個(gè)連接點(diǎn)代表一個(gè)被代理的方法。

為了獲取參數(shù)的方法能夠復(fù)用,這里新建一個(gè)工具類JoinPointUtils:

package com.wl.standard.util;
 
import org.apache.commons.lang.ArrayUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
 
/**
 * JoinPoint 工具類
 * @author wl
 * @date 2022/7/2 21:55
 */
public class JoinPointUtils {
 
    public static <T> T getParamByName(JoinPoint joinPoint, String paramName, Class<T> clazz) {
        // 獲取所有參數(shù)的值
        Object[] args = joinPoint.getArgs();
        // 獲取方法簽名
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        // 在方法簽名中獲取所有參數(shù)的名稱
        String[] parameterNames = methodSignature.getParameterNames();
        // 根據(jù)參數(shù)名稱拿到下標(biāo), 參數(shù)值的數(shù)組和參數(shù)名稱的數(shù)組下標(biāo)是一一對(duì)應(yīng)的
        int index = ArrayUtils.indexOf(parameterNames, paramName);
        // 在參數(shù)數(shù)組中取出下標(biāo)對(duì)應(yīng)參數(shù)值
        Object obj = args[index];
        if (obj == null) {
            return null;
        }
        // 將object對(duì)象轉(zhuǎn)為Class返回
        if (clazz.isInstance(obj)) {
            return clazz.cast(obj);
        }
        return (T) obj;
    }
}

接著編寫@After的增強(qiáng)方法,在切點(diǎn)方法之后執(zhí)行:

package com.wl.standard.aop.aspect;
 
import com.wl.standard.util.JoinPointUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
 
/**
 * @author wl
 * @date 2022/7/2 16:08
 */
@Slf4j
@Component
@Aspect
public class TestAspect {
 
    @Pointcut("execution(public * com.wl.standard.service.TravelRecordService.getAllRecord(..))")
    public void pointCut1(){};
 
    @Pointcut("execution(public * com.wl.standard.service.CityRailService.getTopRail(..))")
    public void pointCut2(){};
 
    @Pointcut("execution(public * com.wl.standard.service.CityGdpService.compareGDP(..))")
    public void pointCut3(){};
 
    @Before("pointCut1()")
    public void doBefore(JoinPoint joinPoint) {
        log.info("當(dāng)前線程: {} 開始執(zhí)行查詢前任務(wù)...", Thread.currentThread().getName());
    }
 
    @After("pointCut2()")
    public void doAfter(JoinPoint joinPoint) {
        Integer index = JoinPointUtils.getParamByName(joinPoint, "index", Integer.class);
        log.info("當(dāng)前線程: {}執(zhí)行完任務(wù),請(qǐng)求參數(shù)值: {}", Thread.currentThread().getName(), index);
    }
}

為了方便理解這里獲取的參數(shù),下面放一下這里切入的方法:

然后一樣的流程,啟動(dòng)項(xiàng)目,在swagger頁(yè)面里調(diào)用接口:

@Around 

前面2個(gè)例子一個(gè)是在切點(diǎn)之前執(zhí)行,一個(gè)是在切點(diǎn)之后執(zhí)行,如果項(xiàng)目中我們想要記錄一個(gè)sql執(zhí)行的耗時(shí)時(shí)間,應(yīng)該怎么做?

@Around環(huán)繞通知:它集成了@Before、@AfterReturing、@AfterThrowing、@After四大通知。需要注意的是,它和其他四大通知注解最大的不同是需要手動(dòng)進(jìn)行接口內(nèi)方法的反射后才能執(zhí)行接口中的方法,換言之,@Around其實(shí)就是一個(gè)動(dòng)態(tài)代理。

利用@Around的話,就可以編寫一個(gè)方法,切入多個(gè)切點(diǎn)記錄耗時(shí)了:

package com.wl.standard.aop.aspect;
 
import com.wl.standard.util.JoinPointUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
 
/**
 * @author wl
 * @date 2022/7/2 16:08
 */
@Slf4j
@Component
@Aspect
public class TestAspect {
 
    @Pointcut("execution(public * com.wl.standard.service.TravelRecordService.getAllRecord(..))")
    public void pointCut1(){};
 
    @Pointcut("execution(public * com.wl.standard.service.CityRailService.getTopRail(..))")
    public void pointCut2(){};
 
    @Pointcut("execution(public * com.wl.standard.service.CityGdpService.compareGDP(..))")
    public void pointCut3(){};
 
    @Before("pointCut1()")
    public void doBefore(JoinPoint joinPoint) {
        log.info("當(dāng)前線程: {} 開始執(zhí)行查詢前任務(wù)...", Thread.currentThread().getName());
    }
 
    @After("pointCut2()")
    public void doAfter(JoinPoint joinPoint) {
        Integer index = JoinPointUtils.getParamByName(joinPoint, "index", Integer.class);
        log.info("當(dāng)前線程: {}執(zhí)行完任務(wù),請(qǐng)求參數(shù)值: {}", Thread.currentThread().getName(), index);
    }
 
    @Around("pointCut3()")
    public Object  doAround(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        // 調(diào)用執(zhí)行目標(biāo)方法(result為目標(biāo)方法執(zhí)行結(jié)果),必須有此行代碼才會(huì)執(zhí)行目標(biāo)調(diào)用的方法(等價(jià)于@befor+@after),否則只會(huì)執(zhí)行一次之前的(等價(jià)于@before)
        Object result = pjp.proceed();
        long end = System.currentTimeMillis();
        log.info(pjp.getTarget().getClass().getSimpleName() + "->" + pjp.getSignature().getName() + " 耗費(fèi)時(shí)間:" + (end - start) + "毫秒");
        return result;
    }
}

啟動(dòng)項(xiàng)目,調(diào)用接口,看控制臺(tái)輸出:

以上就是Spring AOP中三種增強(qiáng)方式的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Spring AOP增強(qiáng)方式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Mybatis的TypeHandler加解密數(shù)據(jù)實(shí)現(xiàn)

    Mybatis的TypeHandler加解密數(shù)據(jù)實(shí)現(xiàn)

    在我們數(shù)據(jù)庫(kù)中有些時(shí)候會(huì)保存一些用戶的敏感信息,所以就需要對(duì)這些數(shù)據(jù)進(jìn)行加密,那么本文就介紹了Mybatis的TypeHandler加解密數(shù)據(jù)實(shí)現(xiàn),感興趣的可以了解一下
    2021-06-06
  • java泛型學(xué)習(xí)示例

    java泛型學(xué)習(xí)示例

    Java泛型(Generics)是JDK5開始引入的一個(gè)新特性,允許在定義類和接口的時(shí)候使用類型參數(shù)(Type Parameter)。下面是學(xué)習(xí)泛型的示例
    2014-04-04
  • Spring Cloud如何切換Ribbon負(fù)載均衡模式

    Spring Cloud如何切換Ribbon負(fù)載均衡模式

    這篇文章主要介紹了Spring Cloud如何切換Ribbon負(fù)載均衡模式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 最新評(píng)論