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
@SpringBootApplicationand allows tests to dynamically contribute exclude filters so that specific classes of component can be excluded.
One interesting example is
@WebMvcTest, because it works thanks to aTypeExcludeFilter:WebMvcTypeExcludeFilterultimately implementsTypeExcludeFilter, which is used to determine if a component/class should not be loaded for this test. Which ones are not included (are excluded)? WellWebMvcTypeExcludeFilterincludes some types by default:In essence, this
WebMvcTypeExcludeFilterwill match any class that is not "included". By matching the filter, the class will be excluded when loading the spring configuration, effectively applying "only configuration relevant to MVC tests" as stated by the JavaDoc.