1

I am trying to understand how ControllerAdvice is working in SpringBoot. It is suggested that for per application there should be one ControllerAdvice. But my question is how this ControllerAdvice is tied to Controllers and catching the exceptions. So basically what is the underhood?

user1474111
  • 1,356
  • 3
  • 23
  • 47

1 Answers1

0

Spring AOP works with proxies.

That is when you annotate a class with any of AOP annotation spring will create a proxy class by extending your annotated class and all the methods will be overridden there in the proxy class.

Thus here after when you call a method in your class spring will call the proxy object method first then your actual method. This is Spring AOP knows whether the method has been called or thrown some exception or returned successfully etc etc.

enter image description here

This is the reason why when you call a private method with in the class AOP cannot intercept that method call.

Vipin CP
  • 3,642
  • 3
  • 33
  • 55
  • So, is it correct that Spring registers classes annotated with @ ControllerAdvice and @ Controller as beans and consider every methods annotated with @ RequestMapping(and its shortcut annotations) as Pointcuts even though there is no explicit @ Pointcut on methods annotated with @ ExceptionHandler in @ ControllerAdvice? – Wood Apr 16 '22 at 14:09