Scan Spring's component classes by annotating @ComponentScan on the class.
Optional parameters for @ComponentScan
- basePackages: Specifies the root package directory that needs to be scanned, and its subdirectories will also be scanned. The default path is the @ComponentScan annotation class directory and its subdirectories
- Alias for value:basePackages
- basePackageClasses: Specifies the root class directory that needs to be scanned, and its subdirectories will also be scanned
- lazyInit: lazy to load, default false
- useDefaultFilters: Whether the auto-scan component is enabled, default true; false means that only the filter rules specified below are applied
- excludeFilters: Specify component types that do not require scanning (see @Filter configuration for details)
- IncudeFilters: Specify the type of component to scan (see @Filter configuration for details)
- scopedProxy: Proxy mode ScopedProxyMode
- DEFAULT: Default, usually NO
- NO: No need to create proxy
- INTERFACES: Create JDK Proxy
- TARGET_CLASS: Create proxy using CGLIB
- nameGenerator: Specifies the class that implements the interface BeanNameGenerator, which can be used to define the generation rules for BeanName
- scopeResolver: Specifies the class that implements the interface AnnotationScopeMetadataResolver, configures the scope, and proxy mode scopedProxy
- resourcePattern: Used to match the appropriate components, default is **/*.class, includeFilters and excludeFilters are recommended
Methods for configuring multiple @ComponentScan s
All three of the following are possible
@ComponentScans( @ComponentScan({"com.xxx.aaa","com.xxx.bbb"}) ) @Configuration public class Config {}
@ComponentScan({"com.xxx.aaa","com.xxx.bbb"}) @Configuration public class Config {}
@ComponentScan({"com.xxx.aaa"}) @ComponentScan({"com.xxx.bbb"}) @Configuration public class Config {}
Configure Filter@Filter
1. Filter @Service annotation classes
@Configuration @ComponentScan( excludeFilters = { @Filter(type = FilterType.ANNOTATION, classes = Service.class) } ) public class Config {}
2. Filter Dog Classes
@Configuration @ComponentScan( excludeFilters = { @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = Dog.class) } ) public class Config {}
3. Regular Matching, Filtering Classes Suffixed with a
@Configuration @ComponentScan( excludeFilters = { @Filter(type = FilterType.REGEX, pattern = {"..*a"}) } ) public class Config {}
4. Customize the filter rule class to filter the class containing a
@Configuration @ComponentScan( excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = MyFilterType.class) } ) public class Config {} // Implement TypeFilter custom rule: filter BeanName containing a public class MyFilterType implements TypeFilter Custom Rules { @Override public boolean match(MetadataReader reader, MetadataReaderFactory factory) throws IOException { return reader.getClassMetadata().getClassName().contains("a"); } }
An example
Config class is configured with the following rules:
- The package paths com.xxx.aaa, com.xxx.bbb need to be scanned
- Exclude the @Service annotation class and the Dog class under the com.xxx.aaa path
- com.xxx.bbb does not enable automatic scanning, but requires the Cat class to be included
@ComponentScan( value = {"com.xxx.aaa"}, excludeFilters = { @Filter(type = FilterType.ANNOTATION, classes = Service.class), @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = Dog.class) } ) @ComponentScan( value = {"com.xxx.bbb"}, useDefaultFilters = false, includeFilters = { @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = Cat.class) } ) @Configuration public class Config { }