In Spring, the annotation @Qualifier specifies which bean is autowired on a field. The following scenario shows an instance where using @Autowired alone is not sufficent.

Autowiring Example
See below example, it will autowired a “person” bean into customer’s person property.

package ie.markreddy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Customer {

	@Autowired
	private Person person;
	//...
}

But, two similar beans “ie.markreddy.Person” are declared in bean configuration file. Will Spring know which person bean should autowired?


When you run above example, it hits below exception :

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
	No unique bean of type [ie.markreddy.Person] is defined:
		expected single matching bean but found 2: [personA, personB]

@Qualifier Example
To fix above problem, you need @Qualifier to tell Spring about which bean should autowired.

package ie.markreddy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Customer {

	@Autowired
	@Qualifier("personA")
	private Person person;
	//...
}

In this case, bean “personA” is autowired.

Customer [person=Person [name=markA]]