spring-framework / org.springframework.web.bind.annotation / ControllerAdvice / <init>

<init>

ControllerAdvice(vararg value: String, basePackages: Array<String>, basePackageClasses: Array<KClass<*>>, assignableTypes: Array<KClass<*>>, annotations: Array<KClass<out Annotation>>)

Specialization of Component for classes that declare ExceptionHandler, InitBinder, or ModelAttribute methods to be shared across multiple @Controller classes.

Classes with @ControllerAdvice can be declared explicitly as Spring beans or auto-detected via classpath scanning. All such beans are sorted via org.springframework.core.annotation.AnnotationAwareOrderComparator, i.e. based on org.springframework.core.annotation.Order and org.springframework.core.Ordered, and applied in that order at runtime. For handling exceptions, an @ExceptionHandler will be picked on the first advice with a matching exception handler method. For model attributes and InitBinder initialization, @ModelAttribute and @InitBinder methods will also follow @ControllerAdvice order.

Note: For @ExceptionHandler methods, a root exception match will be preferred to just matching a cause of the current exception, among the handler methods of a particular advice bean. However, a cause match on a higher-priority advice will still be preferred to a any match (whether root or cause level) on a lower-priority advice bean. As a consequence, please declare your primary root exception mappings on a prioritized advice bean with a corresponding order!

By default the methods in an @ControllerAdvice apply globally to all Controllers. Use selectors #annotations(), #basePackageClasses(), and #basePackages() (or its alias #value()) to define a more narrow subset of targeted Controllers. If multiple selectors are declared, OR logic is applied, meaning selected Controllers should match at least one selector. Note that selector checks are performed at runtime and so adding many selectors may negatively impact performance and add complexity.

Author
Rossen Stoyanchev

Author
Brian Clozel

Author
Sam Brannen

Since
3.2