Spring 4 – Generics Type as Qualifier When Injecting Beans

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

package javabeat.net.beans;

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

Manager.java

package javabeat.net.beans;

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

Admin.java

package javabeat.net.beans;

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

SpringMVCConfiguration.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();
    }

}

InjectBeans.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();
	}
}

SpringMVCApplicationDemo.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();
	}
}

Output

Display : Admin Section
Display : Manager Section

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.

Comments

comments

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.

Speak Your Mind

*