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

詳解Spring AOP自定義可重復(fù)注解沒(méi)有生效問(wèn)題

 更新時(shí)間:2021年08月25日 15:29:08   作者:tobebetter9527  
本文主要介紹了Spring AOP自定義可重復(fù)注解沒(méi)有生效問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

1. 問(wèn)題背景

工作中遇到這樣的場(chǎng)景:某個(gè)方法需要在不同的業(yè)務(wù)場(chǎng)景下執(zhí)行特定的邏輯,該方法已經(jīng)上生產(chǎn),不想改變?cè)瓉?lái)的代碼,因此決定用AOP做個(gè)切面執(zhí)行邏輯。

2. 不啰嗦,上代碼

以下為核心代碼:

定義注解:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Repeatable(value = StartTaskRuns.class)
public @interface StartTaskRun {

  int businessType() default 0;

}

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface StartTaskRuns {

  StartTaskRun[] value();
}

定義切面

@Aspect
@Component
public class StartTaskRunAspect {

  @AfterReturning(pointcut = "@annotation(com.freedom.code.annotation.StartTaskRun)", returning = "retValue")
  public void startTask(JoinPoint joinPoint, Object retValue) throws Exception {
    Object[] args = joinPoint.getArgs();
    Signature signature = joinPoint.getSignature();
    MethodSignature methodSignature = (MethodSignature) signature;
    Method method = methodSignature.getMethod();
    StartTaskRun[] annotations = method.getAnnotationsByType(StartTaskRun.class);
    for (StartTaskRun annotation : annotations) {
      System.out.println(annotation.businessType());
    }
  }
}

業(yè)務(wù)代碼加注解

  @StartTaskRun(businessType = 5)
  @StartTaskRun(businessType = 6)
  @Override
  @Transactional(rollbackFor = Exception.class)
  public String doCsmsStrategy(Long id) {
    // 業(yè)務(wù)邏輯
    return userDO.getId().toString();
  }

debug的時(shí)候發(fā)現(xiàn),切面的代碼沒(méi)有執(zhí)行。

3. 問(wèn)題排查

3.1 是不是切點(diǎn)寫(xiě)得有問(wèn)題,于是換成如下形式:

  @AfterReturning(pointcut = "execution(* com.freedom.code.service.UserServiceImpl.doCsmsStrategy(..))", returning = "retValue")
  public void startTask(JoinPoint joinPoint, Object retValue) throws Exception {
    Object[] args = joinPoint.getArgs();
    Signature signature = joinPoint.getSignature();
    MethodSignature methodSignature = (MethodSignature) signature;
    Method method = methodSignature.getMethod();
    StartTaskRun[] annotations = method.getAnnotationsByType(StartTaskRun.class);
    for (StartTaskRun annotation : annotations) {
      System.out.println(annotation.businessType());
    }
  }

還是不行,但是我的工程中其他地方也是類(lèi)似的寫(xiě)法卻沒(méi)有問(wèn)題啊。看起來(lái)不像是AOP配置不對(duì)的問(wèn)題

3.2 是不是使用的地方不是代理對(duì)象

打斷點(diǎn)吧,如下:

在這里插入圖片描述

是使用cglib生成的代理對(duì)象,沒(méi)有問(wèn)題啊,到底問(wèn)題在哪里。沒(méi)辦法,面向百度編程吧,還真找到問(wèn)題解決辦法。如下帖子:http://www.dbjr.com.cn/article/220762.htm

4. 問(wèn)題原因

對(duì)于可重復(fù)注解,如果方法上用多個(gè)可重復(fù)注解,AOP攔截不到。需要用它的包裝類(lèi)型注解做切點(diǎn),改成以下代碼就可以了:

@Aspect
@Component
public class StartTaskRunAspect {

  @AfterReturning(pointcut = "@annotation(com.freedom.code.annotation.StartTaskRun) || @annotation(com.freedom.code.annotation.StartTaskRuns)", returning = "retValue")
  public void startTask(JoinPoint joinPoint, Object retValue) throws Exception {
    Object[] args = joinPoint.getArgs();
    Signature signature = joinPoint.getSignature();
    MethodSignature methodSignature = (MethodSignature) signature;
    Method method = methodSignature.getMethod();
    StartTaskRun[] annotations = method.getAnnotationsByType(StartTaskRun.class);
    for (StartTaskRun annotation : annotations) {
      System.out.println(annotation.businessType());
    }
  }
}

到此這篇關(guān)于詳解Spring AOP自定義可重復(fù)注解沒(méi)有生效問(wèn)題的文章就介紹到這了,更多相關(guān)Spring AOP注解沒(méi)有生效內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java隊(duì)列數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)

    Java隊(duì)列數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)

    這篇文章主要介紹了Java隊(duì)列數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn),隊(duì)列是一種特殊的線(xiàn)性表,只允許在表的隊(duì)頭進(jìn)行刪除操作,在表的后端進(jìn)行插入操作,隊(duì)列是一個(gè)有序表先進(jìn)先出,想了解更多相關(guān)資料的小伙伴可以參考下面文章的詳細(xì)內(nèi)容
    2021-12-12
  • 記一次Feign中實(shí)現(xiàn)傳實(shí)體Bean的問(wèn)題

    記一次Feign中實(shí)現(xiàn)傳實(shí)體Bean的問(wèn)題

    這篇文章主要介紹了記一次Feign中如何傳實(shí)體Bean的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringMVC參數(shù)的傳遞之如何接收List數(shù)組類(lèi)型的數(shù)據(jù)

    SpringMVC參數(shù)的傳遞之如何接收List數(shù)組類(lèi)型的數(shù)據(jù)

    這篇文章主要介紹了SpringMVC參數(shù)的傳遞之如何接收List數(shù)組類(lèi)型的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Spring StopWatch使用實(shí)例詳解

    Spring StopWatch使用實(shí)例詳解

    這篇文章主要介紹了Spring StopWatch使用實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • java Thread 多線(xiàn)程

    java Thread 多線(xiàn)程

    本篇文章小編為大家介紹,java Thread 多線(xiàn)程。需要的朋友參考下
    2013-04-04
  • java 排序算法之冒泡排序

    java 排序算法之冒泡排序

    這篇文章主要介紹了java 排序算法之冒泡排序,文中運(yùn)用大量的代碼講解相關(guān)知識(shí),非常詳細(xì),感興趣的小伙伴可以參考一下
    2021-09-09
  • java自動(dòng)生成接口文檔完整代碼示例

    java自動(dòng)生成接口文檔完整代碼示例

    在軟件開(kāi)發(fā)中,編寫(xiě)接口文檔是一項(xiàng)必要但繁瑣的任務(wù),為了簡(jiǎn)化這一過(guò)程,可以通過(guò)使用Swagger2和Swagger-UI來(lái)自動(dòng)生成接口文檔,這篇文章主要介紹了java自動(dòng)生成接口文檔的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • MyBatis-Plus與PageHelper依賴(lài)的jsqlparser庫(kù)沖突

    MyBatis-Plus與PageHelper依賴(lài)的jsqlparser庫(kù)沖突

    在升級(jí)SpringBoot到3.x版本的同時(shí),升級(jí)MyBatis-Plus后發(fā)現(xiàn)PageHelper無(wú)法使用,原因是MyBatis-Plus和PageHelper都依賴(lài)jsqlparser庫(kù),且PageHelper要求特定版本的jsqlparser,解決方法是在項(xiàng)目中排除這兩個(gè)庫(kù)的jsqlparser依賴(lài),直接引用jsqlparser4.7版本
    2024-10-10
  • Java 在PPT中創(chuàng)建散點(diǎn)圖的實(shí)現(xiàn)示例

    Java 在PPT中創(chuàng)建散點(diǎn)圖的實(shí)現(xiàn)示例

    本文將以Java代碼示例展示如何在PPT幻燈片中創(chuàng)建散點(diǎn)圖表。文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 淺析Alibaba Nacos注冊(cè)中心源碼剖析

    淺析Alibaba Nacos注冊(cè)中心源碼剖析

    這篇文章主要介紹了淺析Alibaba Nacos注冊(cè)中心源碼剖析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05

最新評(píng)論