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

詳解Spring AOP自定義可重復注解沒有生效問題

 更新時間:2021年08月25日 15:29:08   作者:tobebetter9527  
本文主要介紹了Spring AOP自定義可重復注解沒有生效問題,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

1. 問題背景

工作中遇到這樣的場景:某個方法需要在不同的業(yè)務場景下執(zhí)行特定的邏輯,該方法已經(jīng)上生產(chǎn),不想改變原來的代碼,因此決定用AOP做個切面執(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è)務代碼加注解

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

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

3. 問題排查

3.1 是不是切點寫得有問題,于是換成如下形式:

  @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());
    }
  }

還是不行,但是我的工程中其他地方也是類似的寫法卻沒有問題啊。看起來不像是AOP配置不對的問題

3.2 是不是使用的地方不是代理對象

打斷點吧,如下:

在這里插入圖片描述

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

4. 問題原因

對于可重復注解,如果方法上用多個可重復注解,AOP攔截不到。需要用它的包裝類型注解做切點,改成以下代碼就可以了:

@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());
    }
  }
}

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

相關文章

  • Java隊列數(shù)據(jù)結(jié)構的實現(xiàn)

    Java隊列數(shù)據(jù)結(jié)構的實現(xiàn)

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

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

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

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

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

    Spring StopWatch使用實例詳解

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

    java Thread 多線程

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

    java 排序算法之冒泡排序

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

    java自動生成接口文檔完整代碼示例

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

    MyBatis-Plus與PageHelper依賴的jsqlparser庫沖突

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

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

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

    淺析Alibaba Nacos注冊中心源碼剖析

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

最新評論