In one of my previous posts under Spring Core, I explained the purpose of “@ComponentScan” annotation with example.
For recap, “@ComponentScan” annotation when applied at the class level with an package name as an argument tells Spring framework to scan for class files annotated with “@Component” annotation
under the specified package folder. It scans the subfolders also under the specified package or folder.
For example, the below code tells Spring framework to scan for classes annotated with “@Component” under the base package or folder “package30”.
@ComponentScan(basePackages = "package30")
public class Example30 {
...
}
Now there may be situations where we want to restrict Spring framework to scan only certain subfolders and not all under the base folder.
We can achieve this requirement by using “@ComponentScan.Filter” annotation.
For our example I have created two packages under “package30” as shown in the below figure.

“excludePackage” has class which has to be excluded from scanning and “includePackage” contains class which are to be included when scanning by Spring framework.
Now I will show how to use “@ComponentScan.Filter” annotation. Below is the code snippet
@ComponentScan(basePackages = "package30",
includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "package30.includePackage.*"),
excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "package30.excludePackage.*")
)
public class Example30 {
...
}
So in the above code snippet, in addition to “basePackages” attribute in “@ComponentScan”, I have added two more attributes “includeFilters” and “excludeFilters”.
“includeFilters” and “excludeFilters” attribute is assigned “@ComponentScan.Filter” annotation, that takes two arguments
1) “type” –> indicates the type of filter to use. The values can be one of the following ANNOTATION, ASPECTJ, ASSIGNABLE_TYPE, CUSTOM, and REGEX.
2) “pattern” –> its meaning changes based on the value of “type” argument. For more information refer to javadoc.
So the above code is telling the Spring framework to scan folders and subfolders under “package30.includePackage” folder for classes annotated with “@Component” annotation. Also it is telling Spring
framework to not scan folders and subfolders under “package30.excludePackage” folder for classes annotated with “@Component” annotation.
In this way we can use filters to customize “@ComponentScan” annotation process.
Below is the complete code for your reference
package package30.excludePackage;
import org.springframework.stereotype.Component;
@Component
public class DBDao {
}
package package30.includePackage;
import org.springframework.stereotype.Component;
@Component
public class JMSDao {
}
Main class
package package30;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import package30.excludePackage.DBDao;
import package30.includePackage.JMSDao;
@ComponentScan(basePackages = "package30",
includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "package30.includePackage.*"),
excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "package30.excludePackage.*")
)
public class Example30 {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Example30.class);
JMSDao jmsDao = applicationContext.getBean(JMSDao.class);
System.out.println(jmsDao);
DBDao dbDao = applicationContext.getBean(DBDao.class);
System.out.println(dbDao);
}
}