Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use Autowire With Qualifiers?

    • 0
    • 2
    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 999
    Comment on it

    In Spring we use @Qualifier to indicate which bean we want to qualify to autowired on a field.

    when we create multiple beans of the same type( for example: Department) and want to autowire only one of them with a property in some other class (for example: Employee), in this case we use @Qualifier annotation with @Autowired annotation to remove the confusion by specifying which exact bean will be autowired with that particular property.

    @Qualifier annotation example.

    Department.java

    package com.babita;
    
    public class Department {
    
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return"Department [name="+name+"]";
        }
    }
    

    Employee.java

    package com.babita;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    
    public class Employee {
    
        private int id;
        private String name;
    
        @Autowired
        @Qualifier("dep1")
        private Department department;
    
        public void setDepartment(Department department) {
            this.department = department;
        }
    
        public Department getDepartment() {
            return department;
        }
    
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString(){
        return "Employee [department=" + department + ", id=" + id + ", name="
                + name + "]";
        }
    }
    

    Now define the below code in app-context.xml

    <?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
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <context:annotation-config/>
    
        <bean id="dep" class="com.babita.Department" >
               <property name="name" value="java"></property>
       </bean>
    
       <bean id="dep1" class="com.babita.Department" >
               <property name="name" value="Software"></property>
       </bean>
    
       <bean id="emp" class="com.babita.Employee">
               <property name="name" value="babita"></property>
               <property name="id" value="001"></property>
       </bean>
    
    </beans>
    

    Now define the below class to run the code:

    App.java

    package com.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.babita.Department;
    import com.babita.Employee;
    public class App {
    
        public static void main(String[] args) {
    
            ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"app-context.xml"});
            Employee emp=(Employee)ctx.getBean("emp");
            System.out.println(emp);
    
        }
    }
    

    Output

    Employee [department=Department [name=Software], id=001, name=babita]

    we can see in the above example that two similar beans com.babita.Department are declared in configuration file as dep and dep1 but it is printing the value of dep1 because we have indicated that the bean named dep1 will qualify to autowired in Employee.java file with @Autowired and @Qualifier annotation.

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: