springboot ComponentScan multiple excludeFilters example

springboot

To use excludeFilters with @ComponentScan in Spring Boot, you can use the following example:

@ComponentScan(basePackages = "com.example", 
excludeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.example.package1.*"),
                  @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.example.package2.*")})

This will exclude any components in the package1 and package2 packages from being scanned and registered as beans in the application context.

You can also use the includeFilters attribute to specify which packages should be included in the scan, rather than specifying which packages should be excluded.

@ComponentScan(basePackages = "com.example", 
includeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.example.package3.*"),
                  @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.example.package4.*")})

This will only include components in the package3 and package4 packages in the scan, and all other packages will be excluded.