Thursday 11 May 2017

Spring: Tune autowiring with qualifiers

In my previous post, I explained how @primary annotation is used in autowiring by type with several instances when one primary candidate can be determined. But when you require more control over the selection process, you should use @Qualifier annotation.
 <bean id="osho" class="com.sample.pojo.Author" primary="true">
  <qualifier value="primaryBean" />
  <property name="firstName" value="Chandra Mohan" />
  <property name="lastName" value="Jain" />
  <property name="dateOfBirth" value="11 December 1931" />
  <property name="country" value="India" />
 </bean>

 <bean id="Krishna" class="com.sample.pojo.Author">
  <qualifier value="secondaryBean" />
  <property name="firstName" value="Hari Krishna" />
  <property name="lastName" value="Gurram" />
  <property name="dateOfBirth" value="05 may 1989" />
  <property name="country" value="India" />
 </bean>

Notify above snippet, I added qualifier tag to the beans 'osho' and 'Krishna'. Now I can refer these beans using the qualifier name like below.
public class QualifierDemo {

 @Autowired
 @Qualifier("primaryBean")
 private Author author1;

 @Autowired
 @Qualifier("secondaryBean")
 private Author author2;

}

Following is the complete working application.


Author.java
package com.sample.pojo;

public class Author {
 private String firstName;
 private String lastName;
 private String dateOfBirth;
 private String country;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getDateOfBirth() {
  return dateOfBirth;
 }

 public void setDateOfBirth(String dateOfBirth) {
  this.dateOfBirth = dateOfBirth;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Author [firstName=").append(firstName).append(", lastName=").append(lastName)
    .append(", dateOfBirth=").append(dateOfBirth).append(", country=").append(country).append("]");
  return builder.toString();
 }

}

QualifierDemo.java
package com.sample.test;

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

import com.sample.pojo.Author;

public class QualifierDemo {

 @Autowired
 @Qualifier("primaryBean")
 private Author author1;

 @Autowired
 @Qualifier("secondaryBean")
 private Author author2;

 public Author getAuthor1() {
  return author1;
 }

 public void setAuthor1(Author author1) {
  this.author1 = author1;
 }

 public Author getAuthor2() {
  return author2;
 }

 public void setAuthor2(Author author2) {
  this.author2 = author2;
 }

}

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

 <context:annotation-config />

 <bean id="osho" class="com.sample.pojo.Author" primary="true">
  <qualifier value="primaryBean" />
  <property name="firstName" value="Chandra Mohan" />
  <property name="lastName" value="Jain" />
  <property name="dateOfBirth" value="11 December 1931" />
  <property name="country" value="India" />
 </bean>

 <bean id="Krishna" class="com.sample.pojo.Author">
  <qualifier value="secondaryBean" />
  <property name="firstName" value="Hari Krishna" />
  <property name="lastName" value="Gurram" />
  <property name="dateOfBirth" value="05 may 1989" />
  <property name="country" value="India" />
 </bean>

 <bean id="qualifierDemo" class="com.sample.test.QualifierDemo" />
</beans>

HelloWorld.java
package com.sample.test;

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

import com.sample.pojo.Author;

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

  QualifierDemo demoObj = context.getBean("qualifierDemo", QualifierDemo.class);
  
  Author author1 = demoObj.getAuthor1();
  Author author2 =demoObj.getAuthor2();
  
  System.out.println("author1 = " + author1);
  System.out.println("author2 = " + author2);

  ((ClassPathXmlApplicationContext) context).close();
 }
}

Output
author1 = Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]
author2 = Author [firstName=Hari Krishna, lastName=Gurram, dateOfBirth=05 may 1989, country=India]

The @Qualifier annotation can also be specified on individual constructor arguments or method parameters:

Example
 @Autowired
 public void setAuthor1(@Qualifier("primaryBean") Author author) {
  this.author1 = author;
 }

 @Autowired
 public void setAuthor2(@Qualifier("secondaryBean") Author author) {
  this.author2 = author;
 }

Update ‘QualifierDemo’ class like below and re run HelloWorld.java application, you can able to see same output.


QualifierDemo.java

package com.sample.test;

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

import com.sample.pojo.Author;

public class QualifierDemo {

 private Author author1;

 private Author author2;

 public Author getAuthor1() {
  return author1;
 }

 public Author getAuthor2() {
  return author2;
 }

 @Autowired
 public void setAuthor1(@Qualifier("primaryBean") Author author) {
  this.author1 = author;
 }

 @Autowired
 public void setAuthor2(@Qualifier("secondaryBean") Author author) {
  this.author2 = author;
 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment