• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

@Qualifier Annotation in Spring

April 20, 2013 //  by Manisha Patil//  Leave a Comment

In this post I shall cover another spring annotation @Qualifier which helps fine-tune annotation-based autowiring. In the previous post we saw how we could use @Autowired annotation on setter methods, fields and constructors. There may be scenarios when we create more than one bean of the same type and want to wire only one of them with a property. This can be controlled using @Qualifier annotation along with the @Autowired annotation.

Note : This example written using the XML configurations. If you are starting new spring framework project, please use spring boot for packaging and deploying your spring applications without any burden of writing XML configurations.

Qualifier annotation in spring framework

also read:

  • Spring Tutorials ( Collection for Spring reference tutorials)
  • Spring Framework Interview Questions
  • Introduction to Spring LDAP
  • How to write Custom Spring Callback Methods?

@Qualifier Annotation Example

Let us have working Eclipse IDE in place and follow the following steps to create a Spring application:

  1. Create a project: Create a project with a name SpringAnnotationExamples and create a package com.javabeat.qualifierexample under the src directory in the created project.
  2. Add Libraries: Add required Spring libraries using Add External JARs option as explained in the article Customizing callback methods.
  3. Create source files: Create Java classes Product,Type and MainApp under the com.javabeat.qualifierexample package.
  4. Create configuration file: Create XML based configuration file BeansQualifierAnnotation.xml under src directory.

The class Product.java, is simple POJO class having name,price and an object of Type class. Contents of Product.java are:

 package com.javabeat.qualifierexample;

 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;

 public class Product {
 	private Integer price;
 	private String name;

 	@Autowired
 	@Qualifier("typeB")
 	private Type type;

 	public Integer getPrice() {
 		return price;
 	}

 	public void setPrice(Integer price) {
 		this.price = price;
 	}

 	public Type getType() {
 		return type;
 	}

 	public String getName() {
 		return name;
 	}

 	public void setName(String name) {
 		this.name = name;
 	}
 }
 

Here you can see, I’ve used the @Qualifier annotation alongwith the @Autowired annotation.

The Type.java class is also a POJO class having a string object called “type”. Contents of Type.java are:

 package com.javabeat.autowireexample;

 public class Type {

 	private String type;

 	public String getType() {
 		return type;
 	}

 	public void setType(String type) {
 		this.type = type;
 	}
 }
 

Contents of MainApp are:

 package com.javabeat.qualifierexample;

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

 public class MainApp {
 	public static void main(String[] args) {
 	      ApplicationContext context =
 	             new ClassPathXmlApplicationContext("BeansQualifierAnnotation.xml");

 	      Product product = (Product) context.getBean("product");

 	      System.out.println("Product Name : " + product.getName() );
 	      System.out.println("Price : " + product.getPrice() );

 	      Type productType = product.getType();

 	      System.out.println(product.getName()+" is of type:" + productType.getType() );
 	   }
 }

Contents of BeansQualifierAnnotation.xml are:

<?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.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<context:annotation-config />

	<!-- Definition for Product bean -->
	<bean id="product" class="com.javabeat.qualifierexample.Product">
		<property name="name" value="ProductA" />
		<property name="price" value="400" />
	</bean>
	<bean id="typeA" class="com.javabeat.qualifierexample.Type">
		 <property name="type" value="Export" />
	</bean>
	<bean id="typeB" class="com.javabeat.qualifierexample.Type">
		 <property name="type" value="Import" />
	</bean>

</beans>

As you can note here we are having two beans of same type. In Product.java I’ve used @Qualifier(“typeB”) it means we want to autowire Type property of Product with bean id=”typeB” in XML configuration file.

  • Spring Aspect Oriented Programming (AOP)

Execute the code
The final step is run the application. Once all the above code is ready execute and the below output appears on the console:

Product Name : ProductA
Price : 400
ProductA is of type:Import

Summary

In this post we saw how to configure the Spring container using the @Qualifier annotation. This annotation can be used for on setter methods, on fields, on constructors, with arguments. Examples for the same has been demonstrated. In the next article I shall discuss about JSR-250 Annotations. If you are interested in receiving the future articles, please subscribe here.

If you have any questions on spring framework, please drop comments. I will try my best to respond to your queries.

Category: Spring FrameworkTag: Spring Framework

About Manisha Patil

Manisha S Patil, currently residing at Pune India. She is currently working as freelance writer for websites. She had earlier worked at Caritor Bangalore, TCS Bangalore and Sungard Pune. She has 5 years of experience in Java/J2EE technologies.

Previous Post: « @Autowired Annotation in Spring
Next Post: @Resource, @PostConstruct and @PreDestroy Annotations Example New Features in Spring Boot 1.4»

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact