Categories
Spring Spring Core

Custom Qualifier Annotation

In this article I’m going to explain how to create customized Qualifiers in spring with example code.

Instead of using @Qualifier annotation with name as a parameter everywhere, you can customize the Qualifier annotation for you based on the nature of your application.

For Example : Mine is a Course management application for colleges. Every time I request to Course specific to the Department I need to Specify @Qualifier(“electrical”) or @Qualifier(“computerScience”) in your controller or wherever you need. But if you customized your Qualifier annotation like “Dept” or “Course”, this will be more readable and relatable with your application logic.

We shall look at Example code on how to create Custom @Qualifier annotation in Spring with step by step procedure. 

I’m creating a Maven project in Eclipse with Java 8 and Spring 5.2.6 dependencies. You can copy the below Spring dependencies into your pom.xml file.

<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>5.2.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.2.6.RELEASE</version>
		</dependency>
	</dependencies>

Step 1 : Create Custom Qualifier annotation

I’m creating a Dept interface under the “example” package.

package example;

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})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Dept {
	String value();
}

This is as simple as the above simple code. 

Interface name will act as @Qualifier annotation name and you have to provide one value to the parameter to the annotation.

Step 2 : The equivalent XML Configuration 

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

	<context:component-scan base-package="example" />
	<bean id="courseController" class="example.CourseController"></bean>
	<bean id="computerCourse" class="example.ComputerCourseImpl">
		<qualifier type="example.Dept" value="computerScience"></qualifier>
	</bean>
	<bean id="electricalCourse" class="example.ElectricalCourseImpl">
		<qualifier type="example.Dept" value="electrical"></qualifier>
	</bean>
</beans>

You have to provide a <qualifier/> tag with type and value as mandatory attributes.

Step 3 : Controller

Create controller class and provide newly customized Qualifier annotation to resolve dependencies along with @Autowired annotation.

package example;

import javax.annotation.Resource;

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

public class CourseController {
	@Autowired
	@Dept("computerScience")
	private Course computerCourse;
	private Course electricalCourse;
	
	public void listAllDeptCourses() {
		System.out.println("Computer Dept Courses :"+this.getComputerCourse().listOfCoursesByDept());
		System.out.println("Electrical Dept Courses :"+this.getElectricalCourse().listOfCoursesByDept());
	}
	
	/**
	 * @return the computerCourseImpl
	 */
	public Course getComputerCourse() {
		return computerCourse;
	}

	/**
	 * @param computerCourseImpl the computerCourseImpl to set
	 */
	public void setComputerCourse(Course computerCourse) {
		this.computerCourse = computerCourse;
	}

	/**
	 * @return the electricalCourseImpl
	 */
	public Course getElectricalCourse() {
		return electricalCourse;
	}

	/**
	 * @param electricalCourseImpl the electricalCourseImpl to set
	 */
	@Autowired	
	public void setElectricalCourse(@Dept("electrical") Course electricalCourse) {
		this.electricalCourse = electricalCourse;
	}
}

Step 4 : Interface

Create Course interface and copy below code.

package example;
import java.util.List;

public interface Course {
	List<String> listOfCoursesByDept();
}

Step 5 : Create Implementation classes

ComputerCourseImpl
package example;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

public class ComputerCourseImpl implements Course {

	@Override
	public List<String> listOfCoursesByDept() {
		System.out.println("ComputerCourseImpl :: listOfCoursesByDept");
		return new ArrayList<String>();
	}

}
ElectricalCourseImpl
package example;
import java.util.ArrayList;
import java.util.List;

public class ElectricalCourseImpl implements Course{

	@Override
	public List<String> listOfCoursesByDept() {
		System.out.println("ElectricalCourseImpl :: listOfCoursesByDept");
		return new ArrayList<String>();
	}

}

Step 6 : AppStarter

Create AppStarter class and configure IoC Container as below to instantiate and configure Bean’s.

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

import example.CourseController;

public class AppStarter {

	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CourseController controller = applicationContext.getBean("courseController", CourseController.class);
		controller.listAllDeptCourses();
	}

}

Now run the application from AppStarter you can see the below output logged in console.

That’s all for now on Custom Qualifier annotation in spring with example article…

Happy Reading!!!

Leave a Reply

Your email address will not be published. Required fields are marked *