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)})
Original Post




IncludeFilters with multiple classes.

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

public class BeanConditionScanTest {
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