ControllerAdvice.java 5.2 KB
Newer Older
1
/*
J
Juergen Hoeller 已提交
2
 * Copyright 2002-2018 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 35 36 37 38 39 40
 * 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.
 *
 * <p>Classes with {@code @ControllerAdvice} can be declared explicitly as Spring
 * beans or auto-detected via classpath scanning. All such beans are sorted via
 * {@link org.springframework.core.annotation.AnnotationAwareOrderComparator
 * AnnotationAwareOrderComparator}, i.e. based on
 * {@link org.springframework.core.annotation.Order @Order} and
 * {@link org.springframework.core.Ordered Ordered}, and applied in that order
41 42 43 44 45 46 47 48 49 50 51
 * at runtime. For handling exceptions, an {@code @ExceptionHandler} will be
 * picked on the first advice with a matching exception handler method. For
 * model attributes and {@code InitBinder} initialization, {@code @ModelAttribute}
 * and {@code @InitBinder} methods will also follow {@code @ControllerAdvice} order.
 *
 * <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
 * 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!
52 53 54 55 56 57 58 59 60
 *
 * <p>By default the methods in an {@code @ControllerAdvice} apply globally to
 * all Controllers. Use selectors {@link #annotations()},
 * {@link #basePackageClasses()}, and {@link #basePackages()} (or its alias
 * {@link #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.
61
 *
62
 * @author Rossen Stoyanchev
63
 * @author Brian Clozel
64
 * @author Sam Brannen
65
 * @since 3.2
J
Juergen Hoeller 已提交
66 67
 * @see org.springframework.stereotype.Controller
 * @see RestControllerAdvice
68 69 70 71 72 73 74
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ControllerAdvice {

75
	/**
76 77
	 * Alias for the {@link #basePackages} attribute.
	 * <p>Allows for more concise annotation declarations e.g.:
78
	 * {@code @ControllerAdvice("org.my.pkg")} is equivalent to
79 80
	 * {@code @ControllerAdvice(basePackages="org.my.pkg")}.
	 * @since 4.0
81
	 * @see #basePackages()
82
	 */
83
	@AliasFor("basePackages")
84 85 86 87
	String[] value() default {};

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

100 101 102 103 104 105 106 107 108 109
	/**
	 * Type-safe alternative to {@link #value()} for specifying the packages
	 * to select Controllers to be assisted by the {@code @ControllerAdvice}
	 * 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 {};

110 111
	/**
	 * Array of classes.
112
	 * <p>Controllers that are assignable to at least one of the given types
113 114 115 116 117 118 119
	 * will be assisted by the {@code @ControllerAdvice} annotated class.
	 * @since 4.0
	 */
	Class<?>[] assignableTypes() default {};

	/**
	 * Array of annotations.
120
	 * <p>Controllers that are annotated with this/one of those annotation(s)
121 122 123 124 125
	 * will be assisted by the {@code @ControllerAdvice} annotated class.
	 * <p>Consider creating a special annotation or use a predefined one,
	 * like {@link RestController @RestController}.
	 * @since 4.0
	 */
126 127
	Class<? extends Annotation>[] annotations() default {};

128
}