【问题标题】:AspectJ - Pointcut for all classes in a package except oneAspectJ - 包中所有类的切入点,除了一个
【发布时间】:2019-01-31 17:38:26
【问题描述】:

我目前在记录对我的应用程序中每个服务方法的调用的方法上使用以下切入点:

@Before("execution(* com.mdenis.someAppName..service..*(..))")

有问题的方法使用 Apache 记录器并且运行良好。我现在希望将这些日志记录语句也写入数据库(通过 LogEntryService 类)。问题在于,这实际上是在创建 StackOverflow 错误,因为记录服务包中所有内容的方法正在调用同一包中的方法。

有没有办法改变我的切入点来排除某个类?

【问题讨论】:

    标签: java class spring-boot package aspectj


    【解决方案1】:

    这个怎么样?

    @Before(
      "execution(* com.mdenis.someAppName..service..*(..)) && " + 
      "!within(*..LogEntryService)"
    )
    

    【讨论】:

      【解决方案2】:

      我最终将类切入点与注释切入点结合起来,如下所示:

      @Pointcut("execution(* com.service.processes.communication.CommunicationProcess.*(..))")
      private void communicationMethods() {}
      
      @Pointcut("execution(@com.lib.annotation.Loggable * *.*(..))")
      private void loggableMethods() {}
      
      @Before("communicationMethods() && loggableMethods()")
      public void logMethodCallWithParameters(JoinPoint joinPoint)
      {
          if (joinPoint.getArgs().length == 0)
          {
              logEntryService.logDebug(LogEntrySource.SERVICE, LogEntryType.MESSAGING, "Method " + joinPoint.getTarget().getClass().getCanonicalName() 
                  + "." + joinPoint.getSignature().getName() + " called with no argument", logger);
          }
          else
          {
              logEntryService.logDebug(LogEntrySource.SERVICE, LogEntryType.MESSAGING, "Method " + joinPoint.getTarget().getClass().getCanonicalName() 
                  + "." + joinPoint.getSignature().getName() + " called with argument(s) " + getArgumentString(joinPoint.getArgs()), logger);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-11-17
        • 2014-06-25
        • 1970-01-01
        • 2015-12-06
        • 2015-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多