Knowledge Walls
J2EE Technologies Tutorial
Hyderabad, Andhra Pradesh, India
How to use component-scan with include-filter and exclude-filter in Spring with Example
8159 Views
Hints 
Below is an example of "How to use component-scan with include-filter and exclude-filter in Spring with Example"
JohnPeterEnglandCollege.java
package com.springexample.entities;

import org.springframework.stereotype.Component;

@Component
public class JohnPeterEnglandCollege implements NLLCGroupOfColleges {
	private College college = new College();
	
	public JohnPeterEnglandCollege(){
		college.setCollegeName("John Peter");
		college.setCity("Wasington");
	}

	public College getCollege() {
		return college;
	}
	public void setCollege(College college) {
		this.college = college;
	}	
}
Output 
Bean exist: johnPeterEnglandCollege
Bean exist: JosephCollege
Bean not exist: SmartersCollege
Download as Zip 
Link to download
FiltersOfComponentScan

Hints.
Click on File menu. then click "Download"
Step.1 Start a Java Project with required jars 
  1. Open Eclipse
  2. Click on menu New -> Others
  3. In wizards type "Java Project" and Select "Java Project"
  4. Click Next
  5. Enter project name as "FiltersOfComponentScan", then click Next
  6. Goto Libraries tab, then click on Add External JARs, then select Spring's 21 Framework Jars aopalliance-1.0.jar, aspectjtools-1.6.6.jar and commons-logging-1.1.jar.
  7. Click Finish.
Step.2 Project Explorer Preview 
RunMyProgram.java
package com.springexample;

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

public class RunMyProgram {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		if (context.containsBean("johnPeterEnglandCollege")){
			System.out.println("Bean exist: johnPeterEnglandCollege");
		}
		else {
			System.out.println("Bean not exist: johnPeterEnglandCollege");
		}
		
		if (context.containsBean("josephCollege")){
			System.out.println("Bean exist: JosephCollege");
		}
		else {
			System.out.println("Bean not exist: JosephCollege");
		}
		
		if (context.containsBean("smartersCollege")){
			System.out.println("Bean exist: SmartersCollege");
		}
		else {
			System.out.println("Bean not exist: SmartersCollege");
		}
	}
}
College.java
package com.springexample.entities;

public class College {
	private String collegeName;
	private String city;
	
	public String getCollegeName() {
		return collegeName;
	}
	public void setCollegeName(String collegeName) {
		this.collegeName = collegeName;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	
	public static void display(College college){
		System.out.println("College Details");
		System.out.println(college.getCollegeName());
		System.out.println(college.getCity());
	}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
		
	<context:component-scan base-package="com.springexample.entities">
		<context:include-filter type="assignable" expression="com.springexample.entities.NLLCGroupOfColleges"/>
		<context:exclude-filter type="assignable" expression="com.springexample.entities.SmartersCollege"/>
	</context:component-scan>

</beans>
SmartersCollege.java
package com.springexample.entities;

import org.springframework.stereotype.Component;

@Component
public class SmartersCollege implements NLLCGroupOfColleges {
	private College college = new College();
	
	public SmartersCollege(){
		college.setCollegeName("Smarters College");
		college.setCity("London");
	}

	public College getCollege() {
		return college;
	}
	public void setCollege(College college) {
		this.college = college;
	}	
}
NLLCGroupOfColleges.java
package com.springexample.entities;

public interface NLLCGroupOfColleges {
}
JosephCollege.java
package com.springexample.entities;

import org.springframework.stereotype.Component;

@Component
public class JosephCollege implements NLLCGroupOfColleges {
	private College college = new College();
	
	public JosephCollege(){
		college.setCollegeName("Joseph College");
		college.setCity("Newyork");
	}

	public College getCollege() {
		return college;
	}
	public void setCollege(College college) {
		this.college = college;
	}	
}
Best Lessons of "Spring 3.0 Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details