• +91 9723535972
  • info@interviewmaterial.com

Spring Interview Questions and Answers

Question - What do you understand by @Qualifier annotation?

Answer -

When you create more than one bean of the same type and want to wire only one of them with a property  you can use the @Qualifier annotation along with @Autowired to remove the ambiguity by specifying which exact bean should be wired.

For example, here we have two classes, Employee and EmpAccount respectively. In EmpAccount, using @Qualifier its specified that bean with id emp1 must be wired.

Employee.java

  1. public class Employee
  2. {
  3. private String name;
  4. @Autowired
  5. public void setName(String name)
  6. { this.name=name; }
  7. public string getName()
  8. { return name; }
  9. }
EmpAccount.java

  1. public class EmpAccount
  2. {
  3. private Employee emp;
  4. @Autowired
  5. @Qualifier(emp1)
  6. public void showName()
  7. {
  8. System.out.println(“Employee name : ”+emp.getName);
  9. }
  10. }

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners