By default, @ComponentScan scans all classes which are annotated with @Component, @Repository, @Service, @Controller, @Configuration, or a custom annotation that itself is annotated with @Component.
We can modify and extend this behavior by applying custom filters via @ComponentScan#includeFilters or @ComponentScan#excludeFilters attributes.
Following shows ComponentScan annotation snippet along with include/exclude filter attributes and their type (i.e. Filter ) definition:
package org.springframework.context.annotation;
.....
.....
public @interface ComponentScan {
.....
ComponentScan.Filter[] includeFilters() default {};
ComponentScan.Filter[] excludeFilters() default {};
.....
public @interface Filter {
FilterType type() default FilterType.ANNOTATION;
@AliasFor("classes")
Class<?>[] value() default {};
@AliasFor("value")
Class<?>[] classes() default {};
String[] pattern() default {};
}
}
Following shows FilterType snippet:
package org.springframework.context.annotation;
public enum FilterType {
public enum FilterType {
//Filter candidates marked with a given annotation.
ANNOTATION,
//Filter candidates assignable to a given type.
ASSIGNABLE_TYPE,
//Filter candidates matching a given AspectJ type pattern expression.
ASPECTJ,
//Filter candidates matching a given regex pattern.
REGEX,
//Filter candidates using a given custom TypeFilter implementation.
CUSTOM
}
In next tutorials we will see the example of each (except for ASPECJ which is outside the sope of these tutorials).
|