Junior — Middle
Millistel juhtudel kasutatakse @Qualifier annotatsiooni Springis või sarnastes raamistikudes?
sobes.tech AI
Vastus AI-lt
In Spring, the @Qualifier annotation is used to resolve ambiguities during dependency injection 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 ensures that exactly the bean named "serviceA" will be injected, not another bean of the same type.