【问题标题】:Spring AOP Advice for ResponseEntityExceptionHandler.handleExceptionResponseEntityExceptionHandler.handleException 的 Spring AOP 建议
【发布时间】:2020-11-10 07:50:04
【问题描述】:

我是 Spring AOP 的新手。我尝试为 ResponseEntityExceptionHandler.handleException 方法编写建议以记录异常信息。在寻找解决方案数小时后,我陷入了困境。

这是我的 Apect 组件

@Log4j2
@Aspect
@Component
public class LogginAspect {

  @Pointcut(value = "execution(* org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException(..)) && args(ex, request)")
  public void callSpringExceptionHandler() {}

  @Before("callSpringExceptionHandler()")
  public void logBeforeError(JoinPoint joinPoint, Exception ex, WebRequest request) {
    log.error(ex.getMessage(), ex);
  }
}

我尝试了不同的切入点模式,但没有成功。 我的建议 logBeforeError 根本没有调用过。请帮我解决我的问题

【问题讨论】:

    标签: spring spring-mvc spring-aop


    【解决方案1】:

    来自 Spring 文档:5.8. Proxying Mechanisms

    如果要代理的目标对象至少实现一个接口, 使用 JDK 动态代理。实现的所有接口 目标类型被代理。如果目标对象没有实现任何 接口,创建一个 CGLIB 代理。

    对于 CGLIB,不能建议最终方法,因为它们不能 在运行时生成的子类中被覆盖。

    ResponseEntityExceptionHandler 是一个没有实现任何接口的抽象类,ResponseEntityExceptionHandler.handleException() 是一个最终方法。简而言之,Spring AOP 将无法建议该方法执行。

    不过,您将能够使用 AspectJ 为最终方法提供建议。请通过@kriegaex 的详细answer

    【讨论】:

    • @MadMax,我看到你还是个新手。请注意接受并投票赞成正确的答案,而不是仅仅发布“谢谢”cmets。这也将具有将问题标记为已回答而不是打开的良好副作用。您可以通过单击旁边的灰色复选标记来接受答案,使其变为绿色。通过单击三角形的“向上”按钮完成投票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多