Hints
Below is an example of "How to use component-scan with annotation filter in Spring with Example"
|
JohnPeterEnglandCollege.java
package com.springexample.entities;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope(value="singleton")
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 not exist: JosephCollege
Bean exist: SmartersCollege
|
Download as Zip
Link to download
FiltersOfComponentScanUsingAnnotation
Hints.
Click on File menu. then click " Download"
|
Step.1 Start a Java Project with required jars
- Open Eclipse
- Click on menu New -> Others
- In wizards type "Java Project" and Select "Java Project"
- Click Next
- Enter project name as "FiltersOfComponentScanUsingAnnotation", then click Next
- 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.
- Click Finish.
|
Step.2 Project Explorer Preview
|
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");
}
}
}
|
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());
}
}
|
<?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="annotation" expression="org.springframework.context.annotation.Scope"/>
<context:exclude-filter type="annotation" expression="java.lang.Deprecated"/>
</context:component-scan>
</beans>
|
package com.springexample.entities;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope(value="singleton")
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;
}
}
|
package com.springexample.entities;
public interface NLLCGroupOfColleges {
}
|
package com.springexample.entities;
import org.springframework.stereotype.Component;
@Component
@Deprecated
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;
}
}
|