Share on Google+Share on Google+

Spring filter Bean

In this tutorial you will learn how to filter the component in spring framework.

Spring Filter Bean

The auto scan property can be gain through @Component annotation. The same feature you can  get through filter the components in the xml configuration file. In this example you will see how to filter the components in Spring framework.

package com.roseindia.student.dao;

import org.springframework.stereotype.Component;

public class StudentDAO {
	public String toString() {
		return " Inside StudentDAO";

StudentService .java


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.roseindia.student.dao.StudentDAO;

public class StudentService {
	StudentDAO studentDAO;

	public String toString() {
		return "StudentService [studentDAO=" + studentDAO + "]";

AppMain .java

package com.roseindia.common;

import org.springframework.context.ApplicationContext;


public class AppMain {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "context.xml" });
		StudentService stud = (StudentService) context


<beans xmlns=""
<context:component-scan base-package="com.roseindia" >
  <context:include-filter type="regex" 
              expression="com.roseindia.student.dao.*DAO.*" />
  <context:include-filter type="regex" 
              expression="*Service.*" />
 <context:exclude-filter type="annotation" 
                 expression="org.springframework.stereotype.Service" />  

When you run this application it will display message as shown below:

StudentService [studentDAO= inside StudentDAO]

Download this example code


Posted on: September 2, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.