What's the difference between @Named
and @org.springframework.beans.factory.annotation.Qualifier
in spring annotation configuration?
Which can be used to give a bean name to a bean (just like 'id'
in xml configuration)?
Spring @Primary vs @Qualifier We use @Qualifier in Spring to autowire a specific bean among same type of beans, where as @Primary is used to give high preference to the specific bean among multiple beans of same type to inject to a bean.
JSR 330 @Named annotation is equivalent to spring @Component and @Inject is equivalent to spring @Autowired in spring container with some limitations. A bean annotated with @Named annotation is considered as a component in spring container. We can also provide a name to bean using @Named("anyName") .
The @Primary annotation sets the bean preference and it is used with the @Bean or @Component etc stereotype annotations. On the other hand, @Qualifier is usually used with @Autowired or @Inject etc annotations.
The @Qualifier annotation in Spring is used to differentiate a bean among the same type of bean objects. If we have more than one bean of the same type and want to wire only one of them then use the @Qualifier annotation along with @Autowired to specify which exact bean will be wired.
@Named
is a Java standard (JSR 330), @Qualifier
is used only for Spring; latest versions of Spring recognize both. I'd use @Named
because @Qualifier
is rather used to solve ambiguities where you have two or more beans of the same type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With