In this post, We will learn about the Spring Framework @Qualifier example using a Demo Project.
The @Qualifier
annotation is used to resolve the auto wiring conflict when there are multiple beans of the same type.
The @Qualifier
annotation can be used within the class before any class property. This annotation may also be applied to constructor arguments or method parameters.
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkhindigyan.org</groupId> <artifactId>QualifierAnnotationExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <org.springframework.version>5.2.13.RELEASE</org.springframework.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> </dependencies> </project> |
Employee.java
In this class, you can see we have used @Qualifier (“address2”)(along with @Autowired annotation) annotation before the address property. As we have two Spring beans of com.kkhindigyan.model.Address Type in applicationContext.xml. so to resolve this ambiguity
Here We have used @Qualifier (“address2”) annotation so It will autowire “address2” bean with address property in Employee class, If we do not use @Qualifier annotation here then Spring will throw below Exception
1 2 3 4 |
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employee': Unsatisfied dependency expressed through field 'address'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.kkhindigyan.model.Address' available: expected single matching bean but found 2: address1,address2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.kkhindigyan.model; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; public class Employee { private Integer employeeId; private String employeeName; private Double salary; @Autowired @Qualifier("address2") private Address address; public Integer getEmployeeId() { return employeeId; } public void setEmployeeId(Integer employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public void setAddress(Address address) { this.address = address; } public Address getAddress() { return address; } @Override public String toString() { return "Employee [employeeId=" + employeeId + ", employeeName=" + employeeName + ", salary=" + salary + "]"; } } |
Address.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.kkhindigyan.model; public class Address { private String addressLine1; private String addressLine2; private String city; private Long zipCode; public String getAddressLine1() { return addressLine1; } public void setAddressLine1(String addressLine1) { this.addressLine1 = addressLine1; } public String getAddressLine2() { return addressLine2; } public void setAddressLine2(String addressLine2) { this.addressLine2 = addressLine2; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public Long getZipCode() { return zipCode; } public void setZipCode(Long zipCode) { this.zipCode = zipCode; } @Override public String toString() { return "Address [addressLine1=" + addressLine1 + ", addressLine2=" + addressLine2 + ", city=" + city + ", zipCode=" + zipCode + "]"; } } |
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?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:annotation-config/> <bean id="employee" class="com.kkhindigyan.model.Employee"> <property name="employeeId" value="102883"></property> <property name="employeeName" value="Sean"></property> <property name="salary" value="80000"></property> <!-- <property name="address" ref="address"></property> --> </bean> <bean id="address1" class="com.kkhindigyan.model.Address"> <property name="addressLine1" value="Address Line 1"></property> <property name="addressLine2" value="Address Line 2"></property> <property name="city" value="Delhi"></property> <property name="zipCode" value="959049"></property> </bean> <bean id="address2" class="com.kkhindigyan.model.Address"> <property name="addressLine1" value="Address Line 1"></property> <property name="addressLine2" value="Address Line 2"></property> <property name="city" value="Mumbai"></property> <property name="zipCode" value="9066249"></property> </bean> </beans> |
ClientTest.java
Client Class with the main method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.kkhindigyan.org; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.kkhindigyan.model.Address; import com.kkhindigyan.model.Employee; public class ClientTest { public static void main(String[] args) { ApplicationContext applicationContext = null; try { //Creating Instance of ApplicationContext Spring Container applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //Asking Spring Container to return Spring bean with Specific Id or name Employee employee = (Employee) applicationContext.getBean("employee"); System.out.println(employee); Address address = employee.getAddress(); //Address address = (Address) applicationContext.getBean("address"); System.out.println(address); } catch (Exception e) { e.printStackTrace(); }finally { if (applicationContext != null) ((AbstractApplicationContext) applicationContext).close(); } } } |
If you run ClientTest.java as Java Application then it will give the below output:
1 2 |
Employee [employeeId=102883, employeeName=Sean, salary=80000.0] Address [addressLine1=Address Line 1, addressLine2=Address Line 2, city=Mumbai, zipCode=9066249] |
That’s all about the Spring Framework @Qualifier example
You May Also Like:
Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
Spring Autowiring byName & byType Example
getBean() overloaded methods in Spring Framework
Spring Inner bean example
Spring Dependency Injection with Factory Method
Injecting Collections in Spring Framework Example
Spring Bean Definition Inheritance Example
Spring bean scopes with example
Spring JSR-250 Annotations with Example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?
If you have any feedback or suggestion please feel free to drop in below comment box.