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

Springboot動(dòng)態(tài)配置AOP切點(diǎn)詳解

 更新時(shí)間:2023年09月08日 11:29:25   作者:澄風(fēng)  
這篇文章主要介紹了Springboot動(dòng)態(tài)配置AOP切點(diǎn)詳解,Springboot 可以定義注解切點(diǎn)去攔截注解修飾的類方法以及execution(xxxx)切點(diǎn)去攔截具體的類方法,默認(rèn)情況下我們都會(huì)使用注解@PointCut去定義切點(diǎn),然后定義切面攔截切點(diǎn),需要的朋友可以參考下

前言

Springboot 可以定義注解切點(diǎn)去攔截注解修飾的類方法以及execution (xxxx)切點(diǎn)去攔截具體的類方法。默認(rèn)情況下我們都會(huì)使用注解@PointCut去定義切點(diǎn),然后定義切面攔截切點(diǎn)。

但有些場(chǎng)景需要我們?cè)谂渲梦募?.properties/yml)中配置 execution 靈活的去修改需要攔截的切點(diǎn)。這樣我們可以將一些公共的攔截增強(qiáng)放到 jar 包推送到倉(cāng)庫(kù)中共享。

通用不可動(dòng)態(tài)配置的做法

@Aspect
@Component
public class TransactionConfig {
    private static final String C_POINT_CUT = "execution(public * com.allens.export.service.*.*(..))";
    @Pointcut(C_POINT_CUT)
    public void pointCut () {};
    //@Pointcut("execution(public * com.allens.export.service.*.*(..))")
    //public void pointCut () {};
    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
        System.out.println("----------------");
    }
    @Around("pointCut()")
    public Object process (ProceedingJoinPoint joinPoint) throws Throwable {
        return joinPoint.proceed();
    }
    @After("pointCut()")
    public void after(JoinPoint joinPoint) {
    }
}

可以看到我們可以將切點(diǎn)寫成static final 傳入到注解中,但我們并不能傳變量到切點(diǎn)中。解下來就要講重點(diǎn)部分,如何動(dòng)態(tài)配置切點(diǎn)。其實(shí)我們可以猜到,AOP原理就是使用Java動(dòng)態(tài)代理或CGlib進(jìn)行增強(qiáng)代理。我們使用注解@PointCut定義切點(diǎn)其實(shí)也是需要代碼去解析然后增強(qiáng)。我們其實(shí)也可以直接寫代碼去增強(qiáng)我們的類,手動(dòng)定義切點(diǎn),手動(dòng)定義切面等等。

動(dòng)態(tài)配置切點(diǎn)

AspectJExpressionPointcut 提供表達(dá)式匹配類和方法。

public class GreetingDynamicPointcut extends AspectJExpressionPointcut {
}

MethodInterceptor 提供攔截方法的能力,我們可以借助它實(shí)現(xiàn)注解@Around功能

@Slf4j
public class GreetingInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        try {
            log.info("調(diào)用前...");
            return methodInvocation.proceed();
        } catch (Exception e) {
            log.error("error", e);
            throw e;
        } finally {
            log.info("調(diào)用后...");
        }
    }
}

GreetingAdvice 這個(gè)類繼承了 GreetingInterceptor 可以實(shí)現(xiàn)@Around環(huán)繞功能,繼承了 AfterReturningAdvice 實(shí)現(xiàn)了 @After 方法調(diào)用后攔截功能,繼承了 MethodBeforeAdvice 實(shí)現(xiàn)了方法調(diào)用前的攔截功能。

@Slf4j
public class GreetingAdvice extends GreetingInterceptor implements MethodBeforeAdvice, AfterReturningAdvice, AdvisorAdapter {
    @Override
    public void before(Method method, Object[] args, Object target)
            throws Throwable {
        // 輸出切點(diǎn)
        System.out.println("Pointcut:" + target.getClass().getName() + "."
                + method.getName());
        if (args.length > 0) {
            String clientName = (String) args[0];
            System.out.println("How are you " + clientName + " ?");
        }
    }
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println(returnValue);
    }
    @Override
    public boolean supportsAdvice(Advice advice) {
        return true;
    }
    @Override
    public MethodInterceptor getInterceptor(Advisor advisor) {
        return null;
    }
}

配置

@Configuration
public class WebmvcConfig {
    @Bean
    public Pointcut customPointCut() {
        GreetingDynamicPointcut greetingDynamicPointcut = new GreetingDynamicPointcut();
        // ① 
        greetingDynamicPointcut.setExpression("execution(public * com.allens.test.controller..*(..)) || execution(public * com.allens.test.service..*(..))");
        return greetingDynamicPointcut;
    }
    @Bean
    GreetingBeforeAdvice getAdvice () {
        return new GreetingBeforeAdvice();
    }
	// ②
    @Bean
    DefaultPointcutAdvisor defaultPointcutAdvisor () {
        DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor();
        defaultPointcutAdvisor.setPointcut(customPointCut());
        defaultPointcutAdvisor.setAdvice(getAdvice());
        return defaultPointcutAdvisor;
    }
}

① 我們實(shí)現(xiàn)動(dòng)態(tài)配置只需要把這里的execution 字符串替換成自己從配置文件中獲取的配置即可。

② 自定義切面,可以靈活配置切點(diǎn)和切面。

到此這篇關(guān)于Springboot動(dòng)態(tài)配置AOP切點(diǎn)詳解的文章就介紹到這了,更多相關(guān)Springboot配置AOP切點(diǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring定時(shí)任務(wù)無故停止又不報(bào)錯(cuò)的解決

    Spring定時(shí)任務(wù)無故停止又不報(bào)錯(cuò)的解決

    這篇文章主要介紹了Spring定時(shí)任務(wù)無故停止又不報(bào)錯(cuò)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • mybatis模糊查詢、分頁(yè)和別名配置的方法

    mybatis模糊查詢、分頁(yè)和別名配置的方法

    這篇文章主要介紹了mybatis模糊查詢、分頁(yè)和別名配置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • IDEA插件指南之Mybatis?log插件安裝及使用方法

    IDEA插件指南之Mybatis?log插件安裝及使用方法

    這篇文章主要給大家介紹了關(guān)于IDEA插件指南之Mybatis?log插件安裝及使用的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-02-02
  • springboot實(shí)現(xiàn)執(zhí)行sql語(yǔ)句打印到控制臺(tái)

    springboot實(shí)現(xiàn)執(zhí)行sql語(yǔ)句打印到控制臺(tái)

    這篇文章主要介紹了springboot實(shí)現(xiàn)執(zhí)行sql語(yǔ)句打印到控制臺(tái)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Spring?Data?JPA系列QueryByExampleExecutor使用詳解

    Spring?Data?JPA系列QueryByExampleExecutor使用詳解

    這篇文章主要為大家介紹了Spring?Data?JPA系列QueryByExampleExecutor使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • 詳解JAVA使用Comparator接口實(shí)現(xiàn)自定義排序

    詳解JAVA使用Comparator接口實(shí)現(xiàn)自定義排序

    這篇文章主要介紹了JAVA使用Comparator接口實(shí)現(xiàn)自定義排序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Java多線程之等待隊(duì)列DelayQueue詳解

    Java多線程之等待隊(duì)列DelayQueue詳解

    這篇文章主要介紹了Java多線程之等待隊(duì)列DelayQueue詳解,    DelayQueue被稱作"等待隊(duì)列"或"JDK延遲隊(duì)列",存放著實(shí)現(xiàn)了Delayed接口的對(duì)象,對(duì)象需要設(shè)置到期時(shí)間,當(dāng)且僅當(dāng)對(duì)象到期,才能夠從隊(duì)列中被取走(并非一定被取走),需要的朋友可以參考下
    2023-12-12
  • 如何解決@Valid對(duì)象嵌套List對(duì)象校驗(yàn)無效問題

    如何解決@Valid對(duì)象嵌套List對(duì)象校驗(yàn)無效問題

    這篇文章主要介紹了如何解決@Valid對(duì)象嵌套List對(duì)象校驗(yàn)無效問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • spring?@Transactional注解中常用參數(shù)詳解

    spring?@Transactional注解中常用參數(shù)詳解

    這篇文章主要介紹了spring?@Transactional注解中常用參數(shù)詳解,事物注解方式:?@Transactional,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-02-02
  • Spring的實(shí)例工廠方法和靜態(tài)工廠方法實(shí)例代碼

    Spring的實(shí)例工廠方法和靜態(tài)工廠方法實(shí)例代碼

    這篇文章主要介紹了Spring的實(shí)例工廠方法和靜態(tài)工廠方法實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01

最新評(píng)論