001/* 002 * Copyright 2002-2016 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.context.annotation; 018 019import java.lang.annotation.Documented; 020import java.lang.annotation.ElementType; 021import java.lang.annotation.Repeatable; 022import java.lang.annotation.Retention; 023import java.lang.annotation.RetentionPolicy; 024import java.lang.annotation.Target; 025 026import org.springframework.beans.factory.support.BeanNameGenerator; 027import org.springframework.core.annotation.AliasFor; 028import org.springframework.core.type.filter.TypeFilter; 029 030/** 031 * Configures component scanning directives for use with @{@link Configuration} classes. 032 * Provides support parallel with Spring XML's {@code <context:component-scan>} element. 033 * 034 * <p>Either {@link #basePackageClasses} or {@link #basePackages} (or its alias 035 * {@link #value}) may be specified to define specific packages to scan. If specific 036 * packages are not defined, scanning will occur from the package of the 037 * class that declares this annotation. 038 * 039 * <p>Note that the {@code <context:component-scan>} element has an 040 * {@code annotation-config} attribute; however, this annotation does not. This is because 041 * in almost all cases when using {@code @ComponentScan}, default annotation config 042 * processing (e.g. processing {@code @Autowired} and friends) is assumed. Furthermore, 043 * when using {@link AnnotationConfigApplicationContext}, annotation config processors are 044 * always registered, meaning that any attempt to disable them at the 045 * {@code @ComponentScan} level would be ignored. 046 * 047 * <p>See {@link Configuration @Configuration}'s Javadoc for usage examples. 048 * 049 * @author Chris Beams 050 * @author Juergen Hoeller 051 * @author Sam Brannen 052 * @since 3.1 053 * @see Configuration 054 */ 055@Retention(RetentionPolicy.RUNTIME) 056@Target(ElementType.TYPE) 057@Documented 058@Repeatable(ComponentScans.class) 059public @interface ComponentScan { 060 061 /** 062 * Alias for {@link #basePackages}. 063 * <p>Allows for more concise annotation declarations if no other attributes 064 * are needed — for example, {@code @ComponentScan("org.my.pkg")} 065 * instead of {@code @ComponentScan(basePackages = "org.my.pkg")}. 066 */ 067 @AliasFor("basePackages") 068 String[] value() default {}; 069 070 /** 071 * Base packages to scan for annotated components. 072 * <p>{@link #value} is an alias for (and mutually exclusive with) this 073 * attribute. 074 * <p>Use {@link #basePackageClasses} for a type-safe alternative to 075 * String-based package names. 076 */ 077 @AliasFor("value") 078 String[] basePackages() default {}; 079 080 /** 081 * Type-safe alternative to {@link #basePackages} for specifying the packages 082 * to scan for annotated components. The package of each class specified will be scanned. 083 * <p>Consider creating a special no-op marker class or interface in each package 084 * that serves no purpose other than being referenced by this attribute. 085 */ 086 Class<?>[] basePackageClasses() default {}; 087 088 /** 089 * The {@link BeanNameGenerator} class to be used for naming detected components 090 * within the Spring container. 091 * <p>The default value of the {@link BeanNameGenerator} interface itself indicates 092 * that the scanner used to process this {@code @ComponentScan} annotation should 093 * use its inherited bean name generator, e.g. the default 094 * {@link AnnotationBeanNameGenerator} or any custom instance supplied to the 095 * application context at bootstrap time. 096 * @see AnnotationConfigApplicationContext#setBeanNameGenerator(BeanNameGenerator) 097 */ 098 Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class; 099 100 /** 101 * The {@link ScopeMetadataResolver} to be used for resolving the scope of detected components. 102 */ 103 Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class; 104 105 /** 106 * Indicates whether proxies should be generated for detected components, which may be 107 * necessary when using scopes in a proxy-style fashion. 108 * <p>The default is defer to the default behavior of the component scanner used to 109 * execute the actual scan. 110 * <p>Note that setting this attribute overrides any value set for {@link #scopeResolver}. 111 * @see ClassPathBeanDefinitionScanner#setScopedProxyMode(ScopedProxyMode) 112 */ 113 ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT; 114 115 /** 116 * Controls the class files eligible for component detection. 117 * <p>Consider use of {@link #includeFilters} and {@link #excludeFilters} 118 * for a more flexible approach. 119 */ 120 String resourcePattern() default ClassPathScanningCandidateComponentProvider.DEFAULT_RESOURCE_PATTERN; 121 122 /** 123 * Indicates whether automatic detection of classes annotated with {@code @Component} 124 * {@code @Repository}, {@code @Service}, or {@code @Controller} should be enabled. 125 */ 126 boolean useDefaultFilters() default true; 127 128 /** 129 * Specifies which types are eligible for component scanning. 130 * <p>Further narrows the set of candidate components from everything in {@link #basePackages} 131 * to everything in the base packages that matches the given filter or filters. 132 * <p>Note that these filters will be applied in addition to the default filters, if specified. 133 * Any type under the specified base packages which matches a given filter will be included, 134 * even if it does not match the default filters (i.e. is not annotated with {@code @Component}). 135 * @see #resourcePattern() 136 * @see #useDefaultFilters() 137 */ 138 Filter[] includeFilters() default {}; 139 140 /** 141 * Specifies which types are not eligible for component scanning. 142 * @see #resourcePattern 143 */ 144 Filter[] excludeFilters() default {}; 145 146 /** 147 * Specify whether scanned beans should be registered for lazy initialization. 148 * <p>Default is {@code false}; switch this to {@code true} when desired. 149 * @since 4.1 150 */ 151 boolean lazyInit() default false; 152 153 154 /** 155 * Declares the type filter to be used as an {@linkplain ComponentScan#includeFilters 156 * include filter} or {@linkplain ComponentScan#excludeFilters exclude filter}. 157 */ 158 @Retention(RetentionPolicy.RUNTIME) 159 @Target({}) 160 @interface Filter { 161 162 /** 163 * The type of filter to use. 164 * <p>Default is {@link FilterType#ANNOTATION}. 165 * @see #classes 166 * @see #pattern 167 */ 168 FilterType type() default FilterType.ANNOTATION; 169 170 /** 171 * Alias for {@link #classes}. 172 * @see #classes 173 */ 174 @AliasFor("classes") 175 Class<?>[] value() default {}; 176 177 /** 178 * The class or classes to use as the filter. 179 * <p>The following table explains how the classes will be interpreted 180 * based on the configured value of the {@link #type} attribute. 181 * <table border="1"> 182 * <tr><th>{@code FilterType}</th><th>Class Interpreted As</th></tr> 183 * <tr><td>{@link FilterType#ANNOTATION ANNOTATION}</td> 184 * <td>the annotation itself</td></tr> 185 * <tr><td>{@link FilterType#ASSIGNABLE_TYPE ASSIGNABLE_TYPE}</td> 186 * <td>the type that detected components should be assignable to</td></tr> 187 * <tr><td>{@link FilterType#CUSTOM CUSTOM}</td> 188 * <td>an implementation of {@link TypeFilter}</td></tr> 189 * </table> 190 * <p>When multiple classes are specified, <em>OR</em> logic is applied 191 * — for example, "include types annotated with {@code @Foo} OR {@code @Bar}". 192 * <p>Custom {@link TypeFilter TypeFilters} may optionally implement any of the 193 * following {@link org.springframework.beans.factory.Aware Aware} interfaces, and 194 * their respective methods will be called prior to {@link TypeFilter#match match}: 195 * <ul> 196 * <li>{@link org.springframework.context.EnvironmentAware EnvironmentAware}</li> 197 * <li>{@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware} 198 * <li>{@link org.springframework.beans.factory.BeanClassLoaderAware BeanClassLoaderAware} 199 * <li>{@link org.springframework.context.ResourceLoaderAware ResourceLoaderAware} 200 * </ul> 201 * <p>Specifying zero classes is permitted but will have no effect on component 202 * scanning. 203 * @since 4.2 204 * @see #value 205 * @see #type 206 */ 207 @AliasFor("value") 208 Class<?>[] classes() default {}; 209 210 /** 211 * The pattern (or patterns) to use for the filter, as an alternative 212 * to specifying a Class {@link #value}. 213 * <p>If {@link #type} is set to {@link FilterType#ASPECTJ ASPECTJ}, 214 * this is an AspectJ type pattern expression. If {@link #type} is 215 * set to {@link FilterType#REGEX REGEX}, this is a regex pattern 216 * for the fully-qualified class names to match. 217 * @see #type 218 * @see #classes 219 */ 220 String[] pattern() default {}; 221 222 } 223 224}