Knowledge Walls
J2EE Technologies Tutorial
Hyderabad, Andhra Pradesh, India
Custom Qualifier using @Qualifier annotation of Java Dependency Injection with Spring Example
4071 Views
Hints 
Below is an example of "Custom Qualifier using @Qualifier annotation of Java Dependency Injection with Spring Example"
Download as Zip 
Link to download
AutowiringBeanWithQualifierAnnotationOfJDI

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 "AutowiringBeanWithQualifierAnnotationOfJDI", then click Next
  6. Goto Libraries tab, then click on Add External JARs, then select Spring's 21 Framework Jars javax.inject-1.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");
			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 javax.inject.Inject;

public class JosephCollegeDetails {
	
	@Inject
	@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;
	}
}
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">

	<bean id="joseph" class="com.springexample.College">
		<property name="collegeName" value="Joseph College" />
		<property name="city" value="Newyork" />
		
		<qualifier type="com.springexample.JosephCollegeQualifier" value="josephQualifier" />
	</bean>
	
	<bean id="josephCollegeDetails" class="com.springexample.JosephCollegeDetails">
		<property name="principalName" value="John Peter" />
	</bean> 
	
	<context:annotation-config />

</beans>
JosephCollegeQualifier.java
package com.springexample;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.inject.Qualifier;


@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface JosephCollegeQualifier {
	public String value();
}
Output 
College Details
---------------
Joseph College
Newyork
John Peter
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