ControllerAdvice.java 5.8 KB
Newer Older
1
/*
S
Sam Brannen 已提交
2
 * Copyright 2002-2019 the original author or authors.
3 4 5 6 7
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
S
Spring Operator 已提交
8
 *      https://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15 16 17 18
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.bind.annotation;

19
import java.lang.annotation.Annotation;
20 21 22 23 24 25
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

26
import org.springframework.core.annotation.AliasFor;
27 28 29
import org.springframework.stereotype.Component;

/**
30 31 32 33 34
 * Specialization of {@link Component @Component} for classes that declare
 * {@link ExceptionHandler @ExceptionHandler}, {@link InitBinder @InitBinder}, or
 * {@link ModelAttribute @ModelAttribute} methods to be shared across
 * multiple {@code @Controller} classes.
 *
35 36
 * <p>Classes annotated with {@code @ControllerAdvice} can be declared explicitly
 * as Spring beans or auto-detected via classpath scanning. All such beans are
37
 * sorted based on {@link org.springframework.core.Ordered Ordered} semantics or
38
 * {@link org.springframework.core.annotation.Order @Order} /
39 40 41 42 43 44
 * {@link javax.annotation.Priority @Priority} declarations, with {@code Ordered}
 * semantics taking precedence over {@code @Order} / {@code @Priority} declarations.
 * {@code @ControllerAdvice} beans are then applied in that order at runtime.
 * Note, however, that {@code @ControllerAdvice} beans that implement
 * {@link org.springframework.core.PriorityOrdered PriorityOrdered} are <em>not</em>
 * given priority over {@code @ControllerAdvice} beans that implement {@code Ordered}.
45 46 47 48 49 50
 * In addition, {@code Ordered} is not honored for scoped {@code @ControllerAdvice}
 * beans &mdash; for example if such a bean has been configured as a request-scoped
 * or session-scoped bean.  For handling exceptions, an {@code @ExceptionHandler}
 * will be picked on the first advice with a matching exception handler method. For
 * model attributes and data binding initialization, {@code @ModelAttribute} and
 * {@code @InitBinder} methods will follow {@code @ControllerAdvice} order.
51 52 53 54
 *
 * <p>Note: For {@code @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
S
Sam Brannen 已提交
55
 * advice will still be preferred over any match (whether root or cause level)
56
 * on a lower-priority advice bean. As a consequence, please declare your primary
S
Sam Brannen 已提交
57
 * root exception mappings on a prioritized advice bean with a corresponding order.
58
 *
S
Sam Brannen 已提交
59
 * <p>By default, the methods in an {@code @ControllerAdvice} apply globally to
S
Sam Brannen 已提交
60
 * all controllers. Use selectors such as {@link #annotations},
S
Sam Brannen 已提交
61 62 63 64
 * {@link #basePackageClasses}, and {@link #basePackages} (or its alias
 * {@link #value}) to define a more narrow subset of targeted controllers.
 * If multiple selectors are declared, boolean {@code OR} logic is applied, meaning
 * selected controllers should match at least one selector. Note that selector checks
S
Sam Brannen 已提交
65
 * are performed at runtime, so adding many selectors may negatively impact
66
 * performance and add complexity.
67
 *
68
 * @author Rossen Stoyanchev
69
 * @author Brian Clozel
70
 * @author Sam Brannen
71
 * @since 3.2
J
Juergen Hoeller 已提交
72 73
 * @see org.springframework.stereotype.Controller
 * @see RestControllerAdvice
74 75 76 77 78 79 80
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ControllerAdvice {

81
	/**
82
	 * Alias for the {@link #basePackages} attribute.
83
	 * <p>Allows for more concise annotation declarations &mdash; for example,
84
	 * {@code @ControllerAdvice("org.my.pkg")} is equivalent to
85
	 * {@code @ControllerAdvice(basePackages = "org.my.pkg")}.
86
	 * @since 4.0
87
	 * @see #basePackages
88
	 */
89
	@AliasFor("basePackages")
90 91 92 93
	String[] value() default {};

	/**
	 * Array of base packages.
94
	 * <p>Controllers that belong to those base packages or sub-packages thereof
95 96 97
	 * will be included &mdash; for example,
	 * {@code @ControllerAdvice(basePackages = "org.my.pkg")} or
	 * {@code @ControllerAdvice(basePackages = {"org.my.pkg", "org.my.other.pkg"})}.
98
	 * <p>{@link #value} is an alias for this attribute, simply allowing for
99
	 * more concise use of the annotation.
100
	 * <p>Also consider using {@link #basePackageClasses} as a type-safe
101
	 * alternative to String-based package names.
102 103
	 * @since 4.0
	 */
104
	@AliasFor("value")
105 106
	String[] basePackages() default {};

107
	/**
S
Sam Brannen 已提交
108 109
	 * Type-safe alternative to {@link #basePackages} for specifying the packages
	 * in which to select controllers to be advised by the {@code @ControllerAdvice}
110 111 112 113 114 115 116
	 * annotated class.
	 * <p>Consider creating a special no-op marker class or interface in each package
	 * that serves no purpose other than being referenced by this attribute.
	 * @since 4.0
	 */
	Class<?>[] basePackageClasses() default {};

117 118
	/**
	 * Array of classes.
119
	 * <p>Controllers that are assignable to at least one of the given types
S
Sam Brannen 已提交
120
	 * will be advised by the {@code @ControllerAdvice} annotated class.
121 122 123 124 125
	 * @since 4.0
	 */
	Class<?>[] assignableTypes() default {};

	/**
126 127 128
	 * Array of annotation types.
	 * <p>Controllers that are annotated with at least one of the supplied annotation
	 * types will be advised by the {@code @ControllerAdvice} annotated class.
S
Sam Brannen 已提交
129
	 * <p>Consider creating a custom composed annotation or use a predefined one,
130 131 132
	 * like {@link RestController @RestController}.
	 * @since 4.0
	 */
133 134
	Class<? extends Annotation>[] annotations() default {};

135
}