Close

Spring Framework - @ComponentScan.Filter Examples

Spring Framework 

Scanning only one class in the provided package

@ComponentScan.Filter is used to include or exclude classes from the provided based package. This example will only scan in the provided class (given by Filter#value element), the class is sub-class of ArrayInjectionScanExample class.

@ComponentScan(basePackageClasses = ArrayInjectionScanExample.class,
useDefaultFilters = false,
includeFilters =
{@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
value = ArrayInjectionScanExample.TestBean.class)})




IncludeFilters with multiple classes.

@Configuration
@ComponentScan(basePackageClasses = BeanConditionScanExample.class,
useDefaultFilters = false,
includeFilters = {@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
value = {BeanConditionScanExample.MyClientBean.class,
BeanConditionScanExample.ServiceBeanImpl1.class,
BeanConditionScanExample.ServiceBeanImpl2.class})})

public class BeanConditionScanExample {
Original Post




@Configuration
@ComponentScan(useDefaultFilters = false,
includeFilters = {@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE, classes = {MyBean1.class, MyBean3.class})})
public class FilterTypeAssignableExample {
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(FilterTypeAssignableExample.class);
Util.printBeanNames(context);
}
}
Original Post




@Configuration
@ComponentScan(useDefaultFilters = false,
includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, pattern = ".*1")})
public class FilterTypeAssignableExample2 {

public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(FilterTypeAssignableExample2.class);
Util.printBeanNames(context);
}
}
Original Post
@Configuration
@ComponentScan(useDefaultFilters = false,
includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*[12]"),
excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
classes = FilterTypeAssignableExample2.class)
)
public class FilterTypeRegexExample {

public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(FilterTypeRegexExample.class);
Util.printBeanNames(context);
}
}
Original Post




@Configuration
@ComponentScan(useDefaultFilters = false,
includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = MyAnnotation.class)})
public class FilterTypeAnnotationExample {
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(FilterTypeAnnotationExample.class);
Util.printBeanNames(context);
}
}
Original Post




@Configuration
@ComponentScan(useDefaultFilters = false,
includeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM, classes = MyTypeFilter.class)
)
public class FilterTypeCustomExample {

public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(FilterTypeCustomExample.class);
Util.printBeanNames(context);
}
}
Original Post




See Also