Java @Primary 与 @Autowired 与 @Qualifier 注释的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48447779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between @Primary vs @Autowired with @Qualifier annotations
提问by Maciaz
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?
所以,如果我理解正确的话,如果有多个候选者,两者都是确定要自动装配哪个 bean 的方法。那么究竟有什么区别呢?
采纳答案by Andreas
Read @Primary
as the "default".
读@Primary
作“默认”。
If a bean has @Autowired
withoutany @Qualifier
, and multiple beans of the type exist, the candidate bean marked @Primary
will be chosen, i.e. it is the default selection when no other information is available, i.e. when @Qualifier
is missing.
如果一个 bean@Autowired
没有任何@Qualifier
,并且存在多个该类型的 bean,@Primary
则将选择标记的候选 bean ,即当没有其他信息可用时,即当@Qualifier
缺少时,它是默认选择。
A good use case is that initially you only had one bean of the type, so none of the code used @Qualifier
. When you then add another bean, you then also add @Qualifier
to both the old and the new bean, so any @Autowired
can choose which one it wants. By also adding @Primary
to the old original bean, you don't have to add @Qualifier
to all the existing @Autowired
. They are "grandfathered" in, so to speak.
一个很好的用例是,最初您只有一个该类型的 bean,因此没有任何代码使用@Qualifier
. 当您添加另一个 bean 时,您也同时添加@Qualifier
了旧 bean 和新 bean,因此任何人@Autowired
都可以选择想要的bean 。通过添加@Primary
到旧的原始 bean,您不必添加@Qualifier
到所有现有的@Autowired
. 可以这么说,他们是“祖父”。
@Primary
is also good if e.g. 95% of @Autowired
wants a particular bean. That way, only the @Autowired
that wants the other bean(s) need to specify @Qualifier
. That way, you have primary beans that all autowired wants, and @Qualifier
is only used to request an "alternate" bean.
@Primary
如果例如 95% 的人@Autowired
想要特定的豆子,这也很好。这样,只有@Autowired
需要其他 bean 的 需要指定@Qualifier
. 这样,您就拥有所有自动装配所需的主要 bean,并且@Qualifier
仅用于请求“替代”bean。
回答by pvpkiran
@Qualifier
should be used in conjuction with @Autowired
always. This will indicate the bean name which needs to be Autowired in case of multiple beans with same type is present in the application context.(so that spring can autowire by name.)
@Qualifier
应与@Autowired
always结合使用。如果应用程序上下文中存在多个相同类型的 bean,这将指示需要自动装配的 bean 名称。(以便 spring 可以按名称自动装配。)
@Primary
should be used in conjuction with @Bean
/ @Autowired
which indicates which bean should be given higher preference, when there are multiple beans of same type is present.
@Primary
应该与@Bean
/结合使用@Autowired
,当存在多个相同类型的 bean 时,它指示应该给予哪个 bean 更高的优先级。
One of the classic use cases where you would use @Primary
is when the framework(example spring-data) expects a bean of some type (example EntityManager) but you have multiple datasources and you would have configured multiple Entity Managers. In such cases @Qualifier
doesn't quite help.
您将使用的经典用例之一@Primary
是当框架(例如 spring-data)需要某种类型的 bean(例如 EntityManager)但您有多个数据源并且您将配置多个实体管理器时。在这种情况下@Qualifier
并没有多大帮助。
回答by rimonmostafiz
@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
.
如果一个注入点有多个可用实例,那么我们可以使用@Qualifier
注释来解决歧义。正如@Qualifier
在注入点使用的那样,可能有两种情况我们不想或不能使用@Qualifier
.
- When autowiring mode is
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
- We want to do bean selection (i.e. resolve the ambiguity) at configuration time rather than during beans development time.
- 当自动装配模式为
Autowire.BY_TYPE
. 那么,当然,我们不能使用,@Qualifier
因为我们实际上没有将用户定义的注入点指定为@Autowired
或@Inject
- 我们希望在配置时而不是在 bean 开发期间进行 bean 选择(即解决歧义)。
The solution to above problems is to use @Primary
annotation.
上述问题的解决方案是使用@Primary
注解。
@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.
这表示当多个 bean 是自动装配到单值依赖项的候选者时,应优先考虑特定 bean。如果候选中恰好存在一个“主要”bean,它将是自动装配的值。