spring

Spring qualifier annotation

Hello. In this tutorial, we will create a simple hello world spring boot application and understand the usage of Qualifier annotation.

1. Introduction

@Qualifer annotation in spring is used to identify beans of the same type. It is also used to annotate the custom annotations that behave as qualifiers.

2. Spring boot qualifier annotation

Let us dive into some practice stuff and I am hoping that you are aware of the spring boot basics.

2.1 Tools Used for Spring boot application and Project Structure

We are using IntelliJ, JDK 8, and Maven. In case you’re confused about where you should create the corresponding files or folder, let us review the project structure of the spring boot application.

Fig. 1: Project structure

Let us start building the application!

3. Creating a Spring Boot application

To create the base for this application I will be skipping the traditional approach of creating the project and will move toward the Spring Initializr that offers the quickest way to set up the project. To create this tutorial I will be exploring Maven as the project and Java as the programming language.

3.1 Importing the project

Once the project setup is done via the Spring Initializr import it into the IntelliJ. The imported project will automatically download the jars specified in the pom.xml. For this tutorial, I will use the spring-boot-starter-parent dependency for creating the application.

3.2 Creating the implementation files

3.2.1 Setting the properties

Create a properties file in the resources folder and add the following content to it. The file will contain information about the port number on which the application should run. You’re free to change this information as per your needs.

application.properties

server.port=9500

3.2.2 Creating the Employee model class

To understand the annotation create an Employee class that implements the People interface. @Component is a Spring annotation that allows the Spring container to detect it. @Qualifer("employee") annotation uniquely identifies this bean with the employee name.

Employee.java

package com.example.springbootqualifer.model;

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

@Component
@Qualifier("employee")
public class Employee implements People {

  @Override
  public String designation() {
    return "Employee";
  }
}

3.2.3 Creating the Manager model class

To understand the annotation create a Manager class that implements the People interface. @Component is a Spring annotation that allows the Spring container to detect it. @Qualifier("manager") annotation uniquely identifies this bean with the manager name.

Manager.java

package com.example.springbootqualifer.model;

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

@Component
@Qualifier("manager")
public class Manager implements People {

  @Override
  public String designation() {
    return "Manager";
  }
}

3.2.4 Creating a runner class

Creating the Runner class tells that a bean should run when the Spring application is started. The class is also annotated with the @Component annotation and contains the two person beans injected with @Qualifer annotation.

Runner.java

package com.example.springbootqualifer.runner;

import com.example.springbootqualifer.model.People;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class Runner implements CommandLineRunner {

  @Autowired
  @Qualifier("employee")
  private People p1;

  @Autowired
  @Qualifier("manager")
  private People p2;

  @Override
  public void run(String... args) throws Exception {
    log.info("{}", p1.designation());
    log.info("{}", p2.designation());
  }
}

3.2.5 Creating an implementation class

Creating an implementation class responsible to start the Spring application.

SpringbootqualiferApplication.class

package com.example.springbootqualifer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@Slf4j
@SpringBootApplication
public class SpringbootqualiferApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootqualiferApplication.class, args);
		log.info("application started successfully");
	}
}

4. Run the main class and Demo

To run the application, right-click on the SpringbootqualiferApplication.class class, Run As -> Spring Boot App. If everything goes well the application will be started successfully and the below logs will be generated on the IDE console showing that the person beans were invoked and no duplicate bean exception was observed. In the below logs, the designation information for both the beans (i.e. employee and manager) will be shown.

Logs

2022-07-15 10:33:26.221  INFO 13740 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9500 (http) with context path ''
2022-07-15 10:33:26.242  INFO 13740 --- [           main] c.e.s.SpringbootqualiferApplication      : Started SpringbootqualiferApplication in 5.735 seconds (JVM running for 6.623)
2022-07-15 10:33:26.251  INFO 13740 --- [           main] c.e.springbootqualifer.runner.Runner     : Employee
2022-07-15 10:33:26.253  INFO 13740 --- [           main] c.e.springbootqualifer.runner.Runner     : Manager
2022-07-15 10:33:26.256  INFO 13740 --- [           main] c.e.s.SpringbootqualiferApplication      : application started successfully

That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!

5. Summary

In this tutorial, we see a quick glimpse of @Qualifier annotation in spring and create a simple application to understand the implementation. You can download the sample application as an Eclipse project in the Downloads section.

6. Download the Project

This was an example of @Qualifier annotation in a Spring application.

Download
You can download the full source code of this example here: Spring boot qualifier annotation

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button