Skip to content

Commit fce8ed6

Browse files
committed
Update @ControllerAdvice Javadoc
Issue: SPR-15432
1 parent 9e55e8f commit fce8ed6

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

spring-web/src/main/java/org/springframework/web/bind/annotation/ControllerAdvice.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,28 +26,32 @@
2626
import org.springframework.core.annotation.AliasFor;
2727
import org.springframework.stereotype.Component;
2828

29+
2930
/**
30-
* Indicates the annotated class assists a "Controller".
31-
*
32-
* <p>Serves as a specialization of {@link Component @Component}, allowing for
33-
* implementation classes to be autodetected through classpath scanning.
34-
*
35-
* <p>It is typically used to define {@link ExceptionHandler @ExceptionHandler},
36-
* {@link InitBinder @InitBinder}, and {@link ModelAttribute @ModelAttribute}
37-
* methods that apply to all {@link RequestMapping @RequestMapping} methods.
38-
*
39-
* <p>One of {@link #annotations()}, {@link #basePackageClasses()},
40-
* {@link #basePackages()} or its alias {@link #value()}
41-
* may be specified to define specific subsets of Controllers
42-
* to assist. When multiple selectors are applied, OR logic is applied -
43-
* meaning selected Controllers should match at least one selector.
31+
* Specialization of {@link Component @Component} for classes that declare
32+
* {@link ExceptionHandler @ExceptionHandler}, {@link InitBinder @InitBinder}, or
33+
* {@link ModelAttribute @ModelAttribute} methods to be shared across
34+
* multiple {@code @Controller} classes.
4435
*
45-
* <p>The default behavior (i.e. if used without any selector),
46-
* the {@code @ControllerAdvice} annotated class will
47-
* assist all known Controllers.
36+
* <p>Classes with {@code @ControllerAdvice} can be declared explicitly as Spring
37+
* beans or auto-detected via classpath scanning. All such beans are sorted via
38+
* {@link org.springframework.core.annotation.AnnotationAwareOrderComparator
39+
* AnnotationAwareOrderComparator}, i.e. based on
40+
* {@link org.springframework.core.annotation.Order @Order} and
41+
* {@link org.springframework.core.Ordered Ordered}, and applied in that order
42+
* at runtime. For handling exceptions the first {@code @ExceptionHandler} to
43+
* match the exception is used. For model attributes and {@code InitBinder}
44+
* initialization, {@code @ModelAttribute} and {@code @InitBinder} methods will
45+
* also follow {@code @ControllerAdvice} order.
4846
*
49-
* <p>Note that those checks are done at runtime, so adding many attributes and using
50-
* multiple strategies may have negative impacts (complexity, performance).
47+
* <p>By default the methods in an {@code @ControllerAdvice} apply globally to
48+
* all Controllers. Use selectors {@link #annotations()},
49+
* {@link #basePackageClasses()}, and {@link #basePackages()} (or its alias
50+
* {@link #value()}) to define a more narrow subset of targeted Controllers.
51+
* If multiple selectors are declared, OR logic is applied, meaning selected
52+
* Controllers should match at least one selector. Note that selector checks
53+
* are performed at runtime and so adding many selectors may negatively impact
54+
* performance and add complexity.
5155
*
5256
* @author Rossen Stoyanchev
5357
* @author Brian Clozel

0 commit comments

Comments
 (0)