In wizards type "Java Project" and Select "Java Project"
Click Next
Enter project name as "AutowiringBeanWithCustomQualifier", then click Next
Goto Libraries tab, then click on Add External JARs, then select Spring's 21 Framework Jars and commons-logging-1.1.jar.
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");
JosephCollegeDetails josephCollegeDetails = (JosephCollegeDetails) context.getBean("josephCollegeDetails");
System.out.println("College Details");
System.out.println("---------------");
System.out.println(josephCollegeDetails.getCollege().getCollegeName());
System.out.println(josephCollegeDetails.getCollege().getCity());
System.out.println(josephCollegeDetails.getPrincipalName());
}
}
JosephCollegeDetails.java
package com.springexample;
import org.springframework.beans.factory.annotation.Autowired;
public class JosephCollegeDetails {
@Autowired
@JosephCollegeQualifier(value="josephQualifier")
private College college;
private String principalName;
public College getCollege() {
return college;
}
public void setCollege(College college) {
this.college = college;
}
public String getPrincipalName() {
return principalName;
}
public void setPrincipalName(String principalName) {
this.principalName = principalName;
}
}
College.java
package com.springexample;
public class College {
public String collegeName;
public 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;
}
}