JavaBeat

  • Home
  • 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)
  • Privacy

Spring 4 – Generics Type as Qualifier When Injecting Beans

March 22, 2014 by Krishna Srinivasan Leave a Comment

Generics is one of the most liked feature and most powerful for the Java programming. Spring framework fully supports the generics types for the beans and always try to make use of all the Java programming features like Generics, Annotations, etc. Unfortunately, one problem with earlier versions of Spring was that you could nott use generics as form of qualifier. If you try to @Autowired for generics beans in Spring 3.2, you would get the exception “No qualifying bean of type [Beanname] is defined: expected single matching bean but found 2: benaName1,beanName2“.

One of the improvements introduces in Spring 4 is the generic types as a form of qualifier when injecting Beans. We have several types of methods to determine the closely matching types for the @Autowired annotation. With the Spring 4 release, you can inject the beans using the narrowing of generic types.

Also Read:

  • Spring 4 Tutorials
  • Spring 4 Features

Spring documentation says that:

For example, if you are using a Spring Data Repository you can now easily inject a specific implementation: @Autowired Repository customerRepository.

To demonstrate this feature, I have written a simple example in this tutorial. We have two classes Manager and Admin both are type of Employee class. These two objects are injected using the generic type at run time. Lets look at the example:

Employee.java

[code lang=”java”]
package javabeat.net.beans;

public abstract class Employee<T> {
public void printCompanyName(){
System.out.println("Display : Company Name");
}
public abstract void empSection();
}
[/code]

Manager.java

[code lang=”java”]
package javabeat.net.beans;

public class Manager extends Employee<Manager>{
public void empSection(){
System.out.println("Display : Manager Section");
}
}
[/code]

Admin.java

[code lang=”java”]
package javabeat.net.beans;

public class Admin extends Employee<Admin>{
public void empSection(){
System.out.println("Display : Admin Section");
}
}
[/code]

SpringMVCConfiguration.java

[code lang=”java”]
package javabeat.net.controller;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javabeat.net.beans.Admin;
import javabeat.net.beans.Employee;
import javabeat.net.beans.InjectBeans;
import javabeat.net.beans.Manager;

@Configuration
public class SpringMVCConfiguration {
@Bean
public Employee<Manager> getManager() {
return new Manager();
}

@Bean
public Employee<Admin> getAdmin() {
return new Admin();
}

@Bean(name="InjectBeans")
public InjectBeans getInject() {
return new InjectBeans();
}

}
[/code]

InjectBeans.java

[code lang=”java”]
package javabeat.net.beans;

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

public class InjectBeans {
@Autowired Employee<Manager> emp1;
@Autowired Employee<Admin> emp2;
public void invokeManager(){
emp1.empSection();
}
public void invokeAdmin(){
emp2.empSection();
}
}
[/code]

SpringMVCApplicationDemo.java

[code lang=”java”]
package javabeat.net.controller;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javabeat.net.beans.InjectBeans;

/**
* @Autowired with Generic Types example
*
* @author Krishna
*
*/
public class SpringMVCApplicationDemo {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringMVCConfiguration.class);
InjectBeans injectBeans = (InjectBeans) context.getBean("InjectBeans");
injectBeans.invokeAdmin();
injectBeans.invokeManager();
}
}
[/code]

Output

[code]
Display : Admin Section
Display : Manager Section
[/code]

The above example helps you to understand how to use generics types as the form of qualifier. If you have any questions, please write it in the comments section.

Filed Under: Spring Framework Tagged With: Spring 4

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

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.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved