@@ -4021,27 +4021,25 @@ necessary methods, and declare it as a Spring bean.
40214021=== Controller Advice
40224022[.small]#<<web-reactive.adoc#webflux-ann-controller-advice, WebFlux>>#
40234023
4024- Typically `@ExceptionHandler`, `@InitBinder`, and `@ModelAttribute` methods apply within
4025- the `@Controller` class (or class hierarchy) in which they are declared. If you want such
4026- methods to apply more globally (across controllers), you can declare them in a class
4027- annotated with `@ControllerAdvice` or `@RestControllerAdvice`.
4028-
4029- `@ControllerAdvice` is annotated with `@Component`, which means such classes can be
4030- registered as Spring beans through <<core.adoc#beans-java-instantiating-container-scan,
4031- component scanning>>. `@RestControllerAdvice` is a composed annotation that is annotated
4032- with both `@ControllerAdvice` and `@ResponseBody`, which essentially means
4033- `@ExceptionHandler` methods are rendered to the response body through message conversion
4034- (versus view resolution or template rendering).
4035-
4036- On startup, the infrastructure classes for `@RequestMapping` and `@ExceptionHandler`
4037- methods detect Spring beans annotated with `@ControllerAdvice` and then apply their
4038- methods at runtime. Global `@ExceptionHandler` methods (from a `@ControllerAdvice`) are
4039- applied _after_ local ones (from the `@Controller`). By contrast, global `@ModelAttribute`
4040- and `@InitBinder` methods are applied _before_ local ones.
4041-
4042- By default, `@ControllerAdvice` methods apply to every request (that is, all controllers),
4043- but you can narrow that down to a subset of controllers by using attributes on the
4044- annotation, as the following example shows:
4024+ `@ExceptionHandler`, `@InitBinder`, and `@ModelAttribute` methods apply only to the
4025+ `@Controller` class, or class hierarchy, in which they are declared. If, instead, they
4026+ are declared in an `@ControllerAdvice` or `@RestControllerAdvice` class, then they apply
4027+ to any controller. Moreover, as of 5.3, `@ExceptionHandler` methods in `@ControllerAdvice`
4028+ can be used to handle exceptions from any `@Controller` or any other handler.
4029+
4030+ `@ControllerAdvice` is meta-annotated with `@Component` and therefore can be registered as
4031+ a Spring bean through <<core.adoc#beans-java-instantiating-container-scan,
4032+ component scanning>>. `@RestControllerAdvice` is meta-annotated with `@ControllerAdvice`
4033+ and `@ResponseBody`, and that means `@ExceptionHandler` methods will have their return
4034+ value rendered via response body message conversion, rather than via HTML views.
4035+
4036+ On startup, `RequestMappingHandlerMapping` and `ExceptionHandlerExceptionResolver` detect
4037+ controller advice beans and apply them at runtime. Global `@ExceptionHandler` methods,
4038+ from an `@ControllerAdvice`, are applied _after_ local ones, from the `@Controller`.
4039+ By contrast, global `@ModelAttribute` and `@InitBinder` methods are applied _before_ local ones.
4040+
4041+ The `@ControllerAdvice` annotation has attributes that let you narrow the set of controllers
4042+ and handlers that they apply to. For example:
40454043
40464044[source,java,indent=0,subs="verbatim,quotes",role="primary"]
40474045.Java
0 commit comments