Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between @Named and @Qualifier in spring

Tags:

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)?

like image 399
Tom Sebastian Avatar asked Jul 11 '13 10:07

Tom Sebastian


People also ask

What is the difference between @primary and @qualifier in Spring?

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.

What is @named in Spring?

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") .

What is difference between @primary and @qualifier annotation?

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.

What does @qualifier do in Spring?

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.


1 Answers

@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.

like image 198
morgano Avatar answered Oct 01 '22 18:10

morgano