【发布时间】:2019-10-15 12:12:36
【问题描述】:
我试图了解 ControllerAdvice 在 SpringBoot 中的工作方式。建议每个应用程序应该有一个 ControllerAdvice。但我的问题是这个 ControllerAdvice 如何与控制器相关联并捕获异常。那么基本上什么是底层?
【问题讨论】:
标签: spring spring-boot spring-mvc controller-advice
我试图了解 ControllerAdvice 在 SpringBoot 中的工作方式。建议每个应用程序应该有一个 ControllerAdvice。但我的问题是这个 ControllerAdvice 如何与控制器相关联并捕获异常。那么基本上什么是底层?
【问题讨论】:
标签: spring spring-boot spring-mvc controller-advice
Spring AOP 与代理一起工作。
也就是说,当您 annotate 具有任何 AOP annotation spring 的类将通过 extending 您的注释类创建 proxy class 时,所有方法都将在代理类中被覆盖。
因此,当您在your class 中调用method 之后,spring 将首先调用proxy object method,然后是您的actual method。这是Spring AOP 知道method has been called 或thrown some exception 或returned successfully 等等等等。
这就是为什么当您在class 中调用private method 时AOP 不能intercept 调用该方法。
【讨论】: