001/* 002 * Copyright 2002-2019 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.bind.annotation; 018 019import java.lang.annotation.Annotation; 020import java.lang.annotation.Documented; 021import java.lang.annotation.ElementType; 022import java.lang.annotation.Retention; 023import java.lang.annotation.RetentionPolicy; 024import java.lang.annotation.Target; 025 026import org.springframework.core.annotation.AliasFor; 027import org.springframework.stereotype.Component; 028 029/** 030 * Specialization of {@link Component @Component} for classes that declare 031 * {@link ExceptionHandler @ExceptionHandler}, {@link InitBinder @InitBinder}, or 032 * {@link ModelAttribute @ModelAttribute} methods to be shared across 033 * multiple {@code @Controller} classes. 034 * 035 * <p>Classes annotated with {@code @ControllerAdvice} can be declared explicitly 036 * as Spring beans or auto-detected via classpath scanning. All such beans are 037 * sorted based on {@link org.springframework.core.Ordered Ordered} semantics or 038 * {@link org.springframework.core.annotation.Order @Order} / 039 * {@link javax.annotation.Priority @Priority} declarations, with {@code Ordered} 040 * semantics taking precedence over {@code @Order} / {@code @Priority} declarations. 041 * {@code @ControllerAdvice} beans are then applied in that order at runtime. 042 * Note, however, that {@code @ControllerAdvice} beans that implement 043 * {@link org.springframework.core.PriorityOrdered PriorityOrdered} are <em>not</em> 044 * given priority over {@code @ControllerAdvice} beans that implement {@code Ordered}. 045 * In addition, {@code Ordered} is not honored for scoped {@code @ControllerAdvice} 046 * beans — for example if such a bean has been configured as a request-scoped 047 * or session-scoped bean. For handling exceptions, an {@code @ExceptionHandler} 048 * will be picked on the first advice with a matching exception handler method. For 049 * model attributes and data binding initialization, {@code @ModelAttribute} and 050 * {@code @InitBinder} methods will follow {@code @ControllerAdvice} order. 051 * 052 * <p>Note: For {@code @ExceptionHandler} methods, a root exception match will be 053 * preferred to just matching a cause of the current exception, among the handler 054 * methods of a particular advice bean. However, a cause match on a higher-priority 055 * advice will still be preferred over any match (whether root or cause level) 056 * on a lower-priority advice bean. As a consequence, please declare your primary 057 * root exception mappings on a prioritized advice bean with a corresponding order. 058 * 059 * <p>By default, the methods in an {@code @ControllerAdvice} apply globally to 060 * all controllers. Use selectors such as {@link #annotations}, 061 * {@link #basePackageClasses}, and {@link #basePackages} (or its alias 062 * {@link #value}) to define a more narrow subset of targeted controllers. 063 * If multiple selectors are declared, boolean {@code OR} logic is applied, meaning 064 * selected controllers should match at least one selector. Note that selector checks 065 * are performed at runtime, so adding many selectors may negatively impact 066 * performance and add complexity. 067 * 068 * @author Rossen Stoyanchev 069 * @author Brian Clozel 070 * @author Sam Brannen 071 * @since 3.2 072 * @see org.springframework.stereotype.Controller 073 * @see RestControllerAdvice 074 */ 075@Target(ElementType.TYPE) 076@Retention(RetentionPolicy.RUNTIME) 077@Documented 078@Component 079public @interface ControllerAdvice { 080 081 /** 082 * Alias for the {@link #basePackages} attribute. 083 * <p>Allows for more concise annotation declarations — for example, 084 * {@code @ControllerAdvice("org.my.pkg")} is equivalent to 085 * {@code @ControllerAdvice(basePackages = "org.my.pkg")}. 086 * @since 4.0 087 * @see #basePackages 088 */ 089 @AliasFor("basePackages") 090 String[] value() default {}; 091 092 /** 093 * Array of base packages. 094 * <p>Controllers that belong to those base packages or sub-packages thereof 095 * will be included — for example, 096 * {@code @ControllerAdvice(basePackages = "org.my.pkg")} or 097 * {@code @ControllerAdvice(basePackages = {"org.my.pkg", "org.my.other.pkg"})}. 098 * <p>{@link #value} is an alias for this attribute, simply allowing for 099 * 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 * @since 4.0 103 */ 104 @AliasFor("value") 105 String[] basePackages() default {}; 106 107 /** 108 * Type-safe alternative to {@link #basePackages} for specifying the packages 109 * in which to select controllers to be advised by the {@code @ControllerAdvice} 110 * annotated class. 111 * <p>Consider creating a special no-op marker class or interface in each package 112 * that serves no purpose other than being referenced by this attribute. 113 * @since 4.0 114 */ 115 Class<?>[] basePackageClasses() default {}; 116 117 /** 118 * Array of classes. 119 * <p>Controllers that are assignable to at least one of the given types 120 * will be advised by the {@code @ControllerAdvice} annotated class. 121 * @since 4.0 122 */ 123 Class<?>[] assignableTypes() default {}; 124 125 /** 126 * Array of annotation types. 127 * <p>Controllers that are annotated with at least one of the supplied annotation 128 * types will be advised by the {@code @ControllerAdvice} annotated class. 129 * <p>Consider creating a custom composed annotation or use a predefined one, 130 * like {@link RestController @RestController}. 131 * @since 4.0 132 */ 133 Class<? extends Annotation>[] annotations() default {}; 134 135}