【问题标题】:How can I use @around spring aop annotation on method declaration?如何在方法声明中使用@around spring aop 注解?
【发布时间】:2019-05-28 20:36:29
【问题描述】:

如何在方法声明中使用@around spring AOP 注解?实际上java类中有很多重复的代码,所以我正在考虑优化它。只有@around执行值每次都会改变,3-4个方法的方法定义是相同的。你能建议我能做什么这种情况是为了代码优化?在给定的示例中,您可以看到 nicdStatus 和 nicdPortStatus 只发生了变化,其余所有方法定义都是相同的。 请提供一些代码优化建议,因为我的 java 类中有重复的代码。

@Around("execution(* dcp.casa.services.nicd.NicdController.**nicdStatus**(..)) && args(*, relationId,..)")
Object handleRunTest(final ProceedingJoinPoint joinPoint, final String relationId) {
    log.info("xyz");
    callAbc();
    return joinPoint.proceed();
}

@Around("execution(* dcp.casa.services.nicd.NicdController.nicdPortStatus(..)) && args(*, relationId,..)")
Object handleRunTest(final ProceedingJoinPoint joinPoint, final String relationId) {
    log.info("xyz");
    callAbc();
    return joinPoint.proceed();
}

【问题讨论】:

  • 方法声明?
  • 是的,我可以在方法声明中使用它吗?比如@Around("execution(* dcp.casa.services.nicd.NicdController.nicdStatus(..)) && args(*, relationId,..)") Object handleRunTest(final ProceedingJoinPoint joinPoint, final String relationId);或者做你知道如何优化上面的代码吗?
  • 欢迎来到 SO。你的问题有点不清楚。我猜对了,您有多个具有相同方法主体的 @Around 建议方法,并且您想将这些方法主体分解为辅助方法,以避免您的方面中的代码重复?
  • 是的,你是正确的@Kriegaex。你明白我的问题。

标签: java spring-boot methods spring-aop spring-annotations


【解决方案1】:

AOP 意味着你想拦截一​​些逻辑。在使用 @around 时,您可以在某些方法之前和之后放置一些逻辑。这是删除重复代码的好方法。

你需要做什么:

1) 找出所有重复代码的方法。

2) 将那些重复的代码抽象为一些方法。

3) 使用正确的切入点进行配置。

here 有更多示例。希望能帮上忙。

【讨论】:

    【解决方案2】:

    你的问题有点不清楚。我猜对了,您有多个具有相同方法主体的 @Around 建议方法,并且您想将这些方法主体分解为辅助方法以避免您的方面中的代码重复?

    是的,你是正确的@kriegaex。你明白我的问题。

    那么答案很简单:就像重构任何其他 Java 类一样重构:

    package de.scrum_master.aspect;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    @Aspect
    public class LoggingAspect {
      private static final Logger log = LoggerFactory.getLogger(LoggingAspect.class);
    
      private void callAbc() {}
    
      @Around("execution(* dcp.casa.services.nicd.NicdController.**nicdStatus**(..)) && args(*, relationId, ..)")
      public Object handleRunTestA(ProceedingJoinPoint joinPoint, String relationId) throws Throwable {
        return handleRunHelper(joinPoint);
      }
    
      @Around("execution(* dcp.casa.services.nicd.NicdController.nicdPortStatus(..)) && args(*, relationId, ..)")
      public Object handleRunTestB(ProceedingJoinPoint joinPoint, String relationId) throws Throwable {
        return handleRunHelper(joinPoint);
      }
    
      private Object handleRunHelper(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("xyz");
        callAbc();
        return joinPoint.proceed();
      }
    }
    

    如果辅助方法也需要访问String relationId,只需在其上添加另一个参数并相应调用即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多