Thursday 11 May 2017

Spring: Implement custom qualifiers

You can define custom qualifiers by defining @Qualifier annotation on top of custom annotation class.

Example
         <bean id="Osho" class="com.sample.pojo.Author" primary="true">
                 <qualifier type="AuthorType" 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>

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

Find the complete working application.

AuthorType.java
package com.sample.pojo;

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

import org.springframework.beans.factory.annotation.Qualifier;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.ElementType;

@Target({ ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface AuthorType {
 String value();
}

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 com.sample.pojo.Author;
import com.sample.pojo.AuthorType;

public class QualifierDemo {

 private List<Author> pholosophyAuthors;

 private List<Author> fictionAuthors;

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

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

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

 @Autowired
 public void setFictionAuthors(@AuthorType("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 type="AuthorType" 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 type="AuthorType" 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 type="AuthorType" 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 type="AuthorType" 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 type="AuthorType" 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 type="AuthorType" 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]

Is it possible to define a custom qualifier without value?
Yes, you can define custom qualifier without value.

Example
@Target({ ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface FictionAuthor {

}

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

Find the following 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();
 }

}

PhilosophyAuthor.java
package com.sample.pojo;

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

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

@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface PhilosophyAuthor {

}

FictionAuthor.java
package com.sample.pojo;

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

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

@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface FictionAuthor {

}

QualifierDemo.java
package com.sample.test;

import java.util.List;

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

import com.sample.pojo.Author;
import com.sample.pojo.FictionAuthor;
import com.sample.pojo.PhilosophyAuthor;

public class QualifierDemo {

 private List<Author> pholosophyAuthors;

 private List<Author> fictionAuthors;

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

 @Autowired(required=false)
 @PhilosophyAuthor
 public void setPholosophyAuthors(List<Author> pholosophyAuthors) {
  this.pholosophyAuthors = pholosophyAuthors;
 }

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

 @Autowired(required=false)
 @FictionAuthor
 public void setFictionAuthors(@FictionAuthor 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 type="PhilosophyAuthor"/>
  <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 type="PhilosophyAuthor"/>
  <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 type="PhilosophyAuthor"/>
  <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 type="PhilosophyAuthor"/>
  <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 type="FictionAuthor"/>
  <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 type="FictionAuthor"/>
  <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