Thursday, 11 May 2017

Spring: Applying @Qualifier on collections

By using @Qualifier annotation, you can inject beans of specific type to given collection.

Example
@Autowired
public void setPholosophyAuthors(@Qualifier("philosophy") List<Author> pholosophyAuthors) {
         this.pholosophyAuthors = pholosophyAuthors;
}

Notify above statement, authors of type(qualifier) ‘philosophy’ are injected to the list  ‘pholosophyAuthors’.

Find the below 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 java.util.List;

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

import com.sample.pojo.Author;

public class QualifierDemo {

 private List<Author> pholosophyAuthors;

 private List<Author> fictionAuthors;

 public List<Author> getPholosophyAuthors() {
  return pholosophyAuthors;
 }

 @Autowired
 public void setPholosophyAuthors(@Qualifier("philosophy") List<Author> pholosophyAuthors) {
  this.pholosophyAuthors = pholosophyAuthors;
 }

 public List<Author> getFictionAuthors() {
  return fictionAuthors;
 }

 @Autowired
 public void setFictionAuthors(@Qualifier("fiction") List<Author> fictionAuthors) {
  this.fictionAuthors = fictionAuthors;
 }

}

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="philosophy" />
  <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="Aristotle" class="com.sample.pojo.Author" primary="true">
  <qualifier value="philosophy" />
  <property name="firstName" value="Aristotle" />
  <property name="lastName" value="Aristotle" />
  <property name="dateOfBirth" value="384 BC" />
  <property name="country" value="Northern Greece, Kingdom of Macedon" />
 </bean>

 <bean id="Plato" class="com.sample.pojo.Author" primary="true">
  <qualifier value="philosophy" />
  <property name="firstName" value="Plato" />
  <property name="lastName" value="Plato" />
  <property name="dateOfBirth" value="428 BC" />
  <property name="country" value="Athens, Greece" />
 </bean>

 <bean id="Confucius" class="com.sample.pojo.Author" primary="true">
  <qualifier value="philosophy" />
  <property name="firstName" value="Confucius" />
  <property name="lastName" value="Confucius" />
  <property name="dateOfBirth" value="28 September 551 BC" />
  <property name="country" value="China" />
 </bean>

 <bean id="Shakespeare" class="com.sample.pojo.Author">
  <qualifier value="fiction" />
  <property name="firstName" value="William" />
  <property name="lastName" value="Shakespeare" />
  <property name="dateOfBirth" value="26 April 1564" />
  <property name="country" value="England" />
 </bean>

 <bean id="Simenon" class="com.sample.pojo.Author">
  <qualifier value="fiction" />
  <property name="firstName" value="Georges" />
  <property name="lastName" value="Simenon" />
  <property name="dateOfBirth" value="13 February 1903" />
  <property name="country" value="Belgium" />
 </bean>

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

HelloWorld.java
package com.sample.test;

import java.util.List;

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);

  List<Author> philosophyAuthors = demoObj.getPholosophyAuthors();
  List<Author> fictionAuthors = demoObj.getFictionAuthors();

  System.out.println("Philosophy Authors");
  System.out.println("************************");
  philosophyAuthors.stream().forEach(System.out::println);

  System.out.println("\nFiction Authors");
  System.out.println("************************");
  fictionAuthors.stream().forEach(System.out::println);

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

Output
Philosophy Authors
************************
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]
Author [firstName=Aristotle, lastName=Aristotle, dateOfBirth=384 BC, country=Northern Greece, Kingdom of Macedon]
Author [firstName=Plato, lastName=Plato, dateOfBirth=428 BC, country=Athens, Greece]
Author [firstName=Confucius, lastName=Confucius, dateOfBirth=28 September 551 BC, country=China]

Fiction Authors
************************
Author [firstName=William, lastName=Shakespeare, dateOfBirth=26 April 1564, country=England]
Author [firstName=Georges, lastName=Simenon, dateOfBirth=13 February 1903, country=Belgium]






Previous                                                 Next                                                 Home

No comments:

Post a Comment