So, if I understood correctly, both are the way to determine which bean to autowire if there are multiple candidates. So what exactly is the difference?
@Qualifier
If there are more than one instances available for an injection point then we can use @Qualifier
annotation to resolve an ambiguity. As @Qualifier
is used at the injection point, there might be two situations where we don't want to or cannot use @Qualifier
.
Autowire.BY_TYPE
. Then, of course, we cannot use @Qualifier
because we actually don't have user-defined injection point specified as @Autowired
or @Inject
The solution to above problems is to use @Primary
annotation.
@Primary
This Indicates that a particular bean should be given preference when multiple beans are candidates to be autowired to a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value.