【问题标题】:Spring AOP. How to make pointcut for all public methods in annotated class弹簧 AOP。如何为带注释的类中的所有公共方法创建切入点
【发布时间】:2017-03-24 07:36:03
【问题描述】:

我需要处理从带有一些注释的类的公共方法抛出的所有异常。 我尝试使用 Spring AOP。这是我的记录器:

@Aspect
public class Logger {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Pointcut("@annotation(loggable)")
    public void isLoggable(Loggable loggable) {
    }

    @AfterThrowing(pointcut = "isLoggable(loggable)", throwing = "e")
    public void afterThrowing(Loggable loggable, Exception e) throws Throwable {
        log.error("AFTER", e);
    }

@Loggable 是我的注释。

然后我在我的配置类中添加了@EnableAspectJAutoProxy 注解。

首先,我尝试注释一些引发异常的方法。它工作正常,但我怎样才能使这个工作适用于类中使用@Loggable 注释注释的所有公共方法?

【问题讨论】:

  • 您的 Logger 是 @Bean... @Aspect 不是 @Component,因此不会扫描组件。因此,要么将其注册为 bean,要么添加 @Component 以检测到它,或创建 @ComponentScan 以扫描所有带有 @Aspect 注释的 bean。无论哪种方式,您的方面都必须成为一个 bean,否则它不存在并且什么都不会发生。
  • 您使用@around 建议的解决方案是正确的。尝试调试,检查何时执行建议。
  • @M.Deinum 谢谢!这是一个非常愚蠢的错误。
  • @M.Deinum 如果方法用@Loggable 注释,它可以工作,但我需要方面才能为注释类中的所有公共方法工作
  • @M.Deinum,我知道你总是无私的,不贪心,经常以简单的评论提供正确的答案。但是这样一来,相应的问题就会被列为未回答。您介意在这种情况下写简短的答案以使 OP 接受它们吗?那太好了,在此先感谢。 :-)

标签: java spring spring-aop spring-aspects


【解决方案1】:

您可以像这样创建切面,其中@LogMe 是注解: @Pointcut("execution(@LogMe * *(..))") 匹配所有公共方法。

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;


@Aspect
@Component
public class LogExecutionTime {

    private static final String LOG_MESSAGE_FORMAT = "%s.%s execution time: %dms";
    private static final Logger logger = LoggerFactory.getLogger(LogExecutionTime.class);

    @Pointcut("execution(@LogMe * *(..))")
    public void isAnnotated() {}

    /**
     * Method will add log statement of running time of the methods which are annotated with @LogMe
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("isAnnotated()")
    public Object logTimeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
      StopWatch stopWatch = new StopWatch();
      stopWatch.start();

      Object retVal = joinPoint.proceed();

      stopWatch.stop();

      logExecutionTime(joinPoint, stopWatch);

      return retVal;
    }

    private void logExecutionTime(ProceedingJoinPoint joinPoint, StopWatch stopWatch) {
      String logMessage = String.format(LOG_MESSAGE_FORMAT, joinPoint.getTarget().getClass().getName(), joinPoint.getSignature().getName(), stopWatch.getTotalTimeMillis());
      logger.info(logMessage.toString());
    }
}

【讨论】:

    【解决方案2】:

    带有@Aspect 注释的类不是@Component,因此如果您启用了组件扫描,它将不会被拾取。如果您的上下文中没有 Aspect,则 AOP 没有任何用处。

    要解决此问题,您可以做 3 件事中的 1 件事:

    1. 将@Component 放在@Aspect 旁边
    2. 将@Aspect 定义为@Bean
    3. 添加额外的`@ComponentScan(includeFilter={@Filter(org.aspectj.lang.annotation.Aspect)}

    显然选项#1 是最容易做到的。

    首先我尝试注释一些抛出异常的方法方法。它工作正常,但我怎样才能使这个工作适用于类中使用 @Loggable 注释注释的所有公共方法?

    您需要编写一个与之匹配的切入点。像下面这样的东西应该可以解决问题。

    @Pointcut("public * ((@Loggable *)+).*(..)) && within(@Loggable *)")
    

    一起

    @Pointcut("@Loggable * *(..)")
    

    这将适用于带注释的类中的带注释的方法或公共方法。这是受 Spring Framework 中 AnnotationTransactionAspect 代码的启发。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      相关资源
      最近更新 更多