Recently, Spring Boot added TypeExcludeFilters. One prominent use case is the SpringBootApplication annotation.
Before Spring Boot 1.4:
// ...
@ComponentScan
public @interface SpringBootApplication {
// ...
Since Spring Boot 1.4:
// ...
@ComponentScan(excludeFilters = @Filter(type = FilterType.CUSTOM,
classes = TypeExcludeFilter.class))
public @interface SpringBootApplication {
// ...
The main motivation seems to improve testing support in Spring, but I fail to get an intuitive understanding of what it does and in what situations it can be beneficial.
Can someone illustrate in a simple example how this new concept is intended to be used?
Background: The change came in Spring 1.4.0 with commit 513dec718fd3e7449ec76b6a916f4696d1942d5d:
Add a new TypeFilter specifically for excluding candidate components. The filter is applied to
@SpringBootApplication
and allows tests to dynamically contribute exclude filters so that specific classes of component can be excluded.
Say for some reason (e.g. in integration tests) you don't want some beans (even marked with
@Component
or@Service
) to be registered in application context.This can be achieved by implementing
TypeExcludeFilter
and applying it to test class:For an example of how to implement
TypeExcludeFilter
look at theTestTypeExcludeFilter
.