Spring Filter components in auto scanning

In this Spring auto component scanning tutorial, you learn about how to make Spring auto scan your components. In this article, we show you how to do component filter in auto scanning process.

1. Filter component – include

See following example to use Spring “filtering” to scan and register components’ name which matched defined “regex”, even the class is not annotated with @Component.

DAO layer


package com.mkyong.customer.dao;

public class CustomerDAO 
{
	@Override
	public String toString() {
		return "Hello , This is CustomerDAO";
	}	
}

Service layer


package com.mkyong.customer.services;

import org.springframework.beans.factory.annotation.Autowired;
import com.mkyong.customer.dao.CustomerDAO;

public class CustomerService 
{
	@Autowired
	CustomerDAO customerDAO;

	@Override
	public String toString() {
		return "CustomerService [customerDAO=" + customerDAO + "]";
	}
		
}

Spring filtering.


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<context:component-scan base-package="com.mkyong" >

		<context:include-filter type="regex" 
                       expression="com.mkyong.customer.dao.*DAO.*" />

		<context:include-filter type="regex" 
                       expression="com.mkyong.customer.services.*Service.*" />

	</context:component-scan>

</beans>

Run it


package com.mkyong.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mkyong.customer.services.CustomerService;

public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext context = 
		new ClassPathXmlApplicationContext(new String[] {"Spring-AutoScan.xml"});

    	CustomerService cust = (CustomerService)context.getBean("customerService");
    	System.out.println(cust);
    	
    }
}

Output


CustomerService [customerDAO=Hello , This is CustomerDAO]

In this XML filtering, all files’s name contains DAO or Service (*DAO.*, *Services.*) word will be detect and register in Spring container.

2. Filter component – exclude

On the other hand, you can also exclude specified components, to avoid Spring to detect and register it in Spring container.

Exclude those files annotated with @Service.


	<context:component-scan base-package="com.mkyong.customer" >
		<context:exclude-filter type="annotation" 
			expression="org.springframework.stereotype.Service" />		
	</context:component-scan>

Exclude those files name contains DAO word.


	<context:component-scan base-package="com.mkyong" >
		<context:exclude-filter type="regex" 
			expression="com.mkyong.customer.dao.*DAO.*" />		
	</context:component-scan>

Download Source Code

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
4 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
SurjeetRahul RamrajesantoshDatabase connectivity – Spring+JPA+Hibernate+Derby | Software EverydaySpring Tutorials | Tutorials Recent comment authors
newest oldest most voted
Rahul Ramraje
Guest
Rahul Ramraje

Hi mkyong,
I think you forgot to put @Component in DAO and SERVICE class. Because as per tutorial “Spring Auto scanning components”, it should annotate to search components in base package.
Please confirm.

Surjeet
Guest
Surjeet

Thats why we are using includeFilter, lets say suppose we are working with 3rd party classes where we dont have any source code to modify the class and annotate with @Component, then we can use includeFilter

trackback
Database connectivity – Spring+JPA+Hibernate+Derby | Software Everyday

[…] Spring Filter Components In Auto Scanning. Usually we declare and wire components from the spring xml file. If we want the wiring to happen […]

trackback
Spring Tutorials | Tutorials

[…] Filtering Components in Auto Scanning How to filtering certain components in auto scanning mode. […]

Peter Veentjer
Guest
Peter Veentjer

My experience is that autowiring and component scan doesn’t scale up to larger and more complex systems. It is nice for tutorials and small experiments, but having an explicit written application context is much more maintainable than some crappy magic.