Junior — Middle
In what cases is the @Qualifier annotation used in Spring or similar frameworks?
sobes.tech AI
Answer from AI
The @Qualifier annotation in Spring is used to resolve ambiguities when injecting dependencies, especially when there are multiple beans of the same type in the context. It allows explicitly specifying which bean should be injected.
For example, if you have two beans of type Service:
@Service("serviceA")
public class ServiceA implements Service { }
@Service("serviceB")
public class ServiceB implements Service { }
And you want to inject a specific bean, use @Qualifier:
@Autowired
@Qualifier("serviceA")
private Service service;
This guarantees that the bean named "serviceA" will be injected, rather than another bean of the same type.