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.annotation.Order @Order} 038 * semantics and applied in that order at runtime. For handling exceptions, an 039 * {@code @ExceptionHandler} will be picked on the first advice with a matching 040 * exception handler method. For model attributes and {@code InitBinder} 041 * initialization, {@code @ModelAttribute} and {@code @InitBinder} methods will 042 * also follow {@code @ControllerAdvice} order. 043 * 044 * <p>Note: For {@code @ExceptionHandler} methods, a root exception match will be 045 * preferred to just matching a cause of the current exception, among the handler 046 * methods of a particular advice bean. However, a cause match on a higher-priority 047 * advice will still be preferred over any match (whether root or cause level) 048 * on a lower-priority advice bean. As a consequence, please declare your primary 049 * root exception mappings on a prioritized advice bean with a corresponding order. 050 * 051 * <p>By default the methods in an {@code @ControllerAdvice} apply globally to 052 * all controllers. Use selectors such as {@link #annotations}, 053 * {@link #basePackageClasses}, and {@link #basePackages} (or its alias 054 * {@link #value}) to define a more narrow subset of targeted controllers. 055 * If multiple selectors are declared, {@code OR} logic is applied, meaning selected 056 * controllers should match at least one selector. Note that selector checks 057 * are performed at runtime, so adding many selectors may negatively impact 058 * performance and add complexity. 059 * 060 * @author Rossen Stoyanchev 061 * @author Brian Clozel 062 * @author Sam Brannen 063 * @since 3.2 064 */ 065@Target(ElementType.TYPE) 066@Retention(RetentionPolicy.RUNTIME) 067@Documented 068@Component 069public @interface ControllerAdvice { 070 071 /** 072 * Alias for the {@link #basePackages} attribute. 073 * <p>Allows for more concise annotation declarations e.g.: 074 * {@code @ControllerAdvice("org.my.pkg")} is equivalent to 075 * {@code @ControllerAdvice(basePackages="org.my.pkg")}. 076 * @since 4.0 077 * @see #basePackages() 078 */ 079 @AliasFor("basePackages") 080 String[] value() default {}; 081 082 /** 083 * Array of base packages. 084 * <p>Controllers that belong to those base packages or sub-packages thereof 085 * will be included, e.g.: {@code @ControllerAdvice(basePackages="org.my.pkg")} 086 * or {@code @ControllerAdvice(basePackages={"org.my.pkg", "org.my.other.pkg"})}. 087 * <p>{@link #value} is an alias for this attribute, simply allowing for 088 * more concise use of the annotation. 089 * <p>Also consider using {@link #basePackageClasses()} as a type-safe 090 * alternative to String-based package names. 091 * @since 4.0 092 */ 093 @AliasFor("value") 094 String[] basePackages() default {}; 095 096 /** 097 * Type-safe alternative to {@link #value()} for specifying the packages 098 * to select Controllers to be assisted by the {@code @ControllerAdvice} 099 * annotated class. 100 * <p>Consider creating a special no-op marker class or interface in each package 101 * that serves no purpose other than being referenced by this attribute. 102 * @since 4.0 103 */ 104 Class<?>[] basePackageClasses() default {}; 105 106 /** 107 * Array of classes. 108 * <p>Controllers that are assignable to at least one of the given types 109 * will be assisted by the {@code @ControllerAdvice} annotated class. 110 * @since 4.0 111 */ 112 Class<?>[] assignableTypes() default {}; 113 114 /** 115 * Array of annotations. 116 * <p>Controllers that are annotated with this/one of those annotation(s) 117 * will be assisted by the {@code @ControllerAdvice} annotated class. 118 * <p>Consider creating a special annotation or use a predefined one, 119 * like {@link RestController @RestController}. 120 * @since 4.0 121 */ 122 Class<? extends Annotation>[] annotations() default {}; 123 124}