Updated April 1, 2023
Definition of Spring Boot Qualifier
Spring boot qualifier is used when we need to create more than one bean for the same type and need to wired-only one bean. In this situation, we are using autowired and qualifier annotation to remove confusion which bean we need to wired in our spring boot project. Basically, it is showing how to differentiate bean of same type which is qualifier.
What is Spring Boot Qualifier?
- As we know that autowired annotation is mostly used in injecting the spring boot dependency.
- By default, the annotation of autowired will resolve its dependencies. This annotation is working well if we have only one bean of the same type.
- Spring boot framework is throwing an exception when we have used the same type of bean one or more times.
- Spring boot qualifier annotation is used to distinguish the references of bean. For selecting the correct bean from code we are using qualifier annotation in spring boot project.
- Spring boot qualifier annotation bean is also used to eliminate the issue which was injected by beans.
- We are using bean in one class to eliminate the issue of which beans to be injected from project.
The below example shows how to solve the issue of bean injection in our project are as follows.
Example:
Public class qualifier
{
@Autowired
@Qualifier ("SpringBootQualifier")
Private SBQualifier squalifier
}
• In the above example, we can see that we have used qualifier annotation with the implementation name as SpringBootQualifier.
• In the above example we have avoided the ambiguity when spring boot will find multiple beans of a single type.
• To use same bean in single code we need to use @component annotation to obtain same result in program.
• Below example shows how to use two qualifier annotation in single code are as follows.
Example:
@Component
@Qualifier ("SpringBootQualifier")
Public class SpringBootQualifier implements SBQualifier
{
….
}
@Component
@Qualifier ("SBQualifier")
Public class sbQualifier implements SBQualifier
{
….
}
- We are using another annotation name as primary which was deciding which bean we need to inject when the project contains the ambiguity.
Project structure of the Spring Boot application
The below example shows the project structure of the spring boot application is as follows. In the below application, we have defined the bean as per and type as stud and mngr. We are using qualifier annotation in our project to distinguish the same.
Pom.xml
|
|---main class
|
|---Java
|---- code of project
| Application.java file
| MyRunner.java file
|
|------ model
Mngr.java file
Per.java file
Stud.java file
|----- resources
|------test
|----java
- We are using spring boot starter and spring boot maven plugin dependency to develop applications using spring boot qualifier.
- In the above example, we have to inherit stud class from per class. We have used @component annotation which was allowing studs to detect by using a container of spring.
Uses of qualifier in spring boot
- Qualifier annotation is used to help auto wiring which was annotation basis. There is multiple scenarios where we are creating more than a single bean, at the same time we are using qualifier annotation in the spring boot project.
- We can control the bean injection situation by using @Qualifier annotation in the spring boot application.
- In the spring boot application, @Qualifier annotation is used to resolve ambiguous dependencies. Also, @Qualifier annotation helps us to @Autowired annotation to choose one of the annotations from dependency.
- If suppose our spring boot application contains multiple implementations of one interface same time we are using @Qualifier annotation to select useful implementation of code at runtime.
- Also we can say that qualifier annotation is used to resolve the conflict of auto wiring when we are using multiple types of beans in single class.
- We can use @Qualifier annotation in any class which was annotated by @component and the method is annotated as @Beam.
- We can also apply @Qualifier annotation on method parameters and on constructor arguments.
- We are using @Qualifier annotation in the spring boot application. We can use qualifier bean to choose correct and suitable bean of our application.
- We can handle multiple beans issues by using @Qualifier. We can annotate other custom annotation by using @Qualifier annotation which was used as qualifier in our project.
Spring boot Qualifier Example
Below is the example of spring boot qualifiers is as follows.
1) Create project template using spring initializer and give name to project –
In the below step we have providing project group name as com. example, artifact name as springbootqualifier, project name as springbootqualifier, and selected java version as 8.
Group – com. example
Artifact name – springbootqualifier
Name – springbootqualifier
Spring boot – 2.6.0
Project – Maven
Project Description – Project for springbootqualifier
Java – 8
Dependencies – spring data JPA
Package name – com.example.springbootqualifier
2) After generating project extract files and open this project by using spring tool suite –
- After generating the project by using spring initializer in this step we are extracting the jar file and opening project by using spring tool suite.
3) After opening project using spring tool suite check the project and its files –
In this step, we are checking all the project template files. We also need to check maven dependencies and system libraries.
4) Add dependency packages –
In this step, we are adding required dependency in our project.
Code:
<dependency> -- Start of dependency tag.
<groupId>org.springframework.boot</groupId> -- Start and end of groupId tag.
<artifactId>spring-boot-starter-maven-plugin</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.
5) Create stud.java file –
Code:
public class stud {
private Integer stud_id;
public void setID (Integer stud_id) {
this.stud_id = stud_id;
}
public Integer getID () {
return stud_id;
}
}
6) Create stud_profile.java file –
Code:
public class stud_profile {
@Autowired
@Qualifier("stud1")
private stud st;
public stud_profile (){
System.out.println("Stud profile constructor." );
}
public void printAge () {
System.out.println("stud_id : " + stud.getID () );
}
}
7) Create main.java file –
Code:
public class main {
ApplicationContext cont = new ClassPathXmlApplicationContext ("Beans.xml");
stud_profile pro = (stud_profile) cont.getBean ("stud_profile");
pro.printID ();
}
}
8) Run the application –
Conclusion
We are using to spring boot qualifier bean in one class to eliminate the issue of which beans to be injected from project. Spring boot qualifier is used when we need to create more than one bean of same type and need to wired-only one bean.
Recommended Articles
This is a guide to Spring Boot Qualifier. Here we discuss definition, What is Spring Boot Qualifier, Project structure, examples with code implementation. You may also have a look at the following articles to learn more –