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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:50:58  来源:igfitidea点击:

Difference between @Primary vs @Autowired with @Qualifier annotations

javaspringannotations

提问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 @Primaryas the "default".

@Primary作“默认”。

If a bean has @Autowiredwithoutany @Qualifier, and multiple beans of the type exist, the candidate bean marked @Primarywill be chosen, i.e. it is the default selection when no other information is available, i.e. when @Qualifieris 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 @Qualifierto both the old and the new bean, so any @Autowiredcan choose which one it wants. By also adding @Primaryto the old original bean, you don't have to add @Qualifierto all the existing @Autowired. They are "grandfathered" in, so to speak.

一个很好的用例是,最初您只有一个该类型的 bean,因此没有任何代码使用@Qualifier. 当您添加另一个 bean 时,您也同时添加@Qualifier了旧 bean 和新 bean,因此任何人@Autowired都可以选择想要的bean 。通过添加@Primary到旧的原始 bean,您不必添加@Qualifier到所有现有的@Autowired. 可以这么说,他们是“祖父”。

@Primaryis also good if e.g. 95% of @Autowiredwants a particular bean. That way, only the @Autowiredthat wants the other bean(s) need to specify @Qualifier. That way, you have primary beans that all autowired wants, and @Qualifieris only used to request an "alternate" bean.

@Primary如果例如 95% 的人@Autowired想要特定的豆子,这也很好。这样,只有@Autowired需要其他 bean 的 需要指定@Qualifier. 这样,您就拥有所有自动装配所需的主要 bean,并且@Qualifier仅用于请求“替代”bean。

回答by pvpkiran

@Qualifiershould be used in conjuction with @Autowiredalways. 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应与@Autowiredalways结合使用。如果应用程序上下文中存在多个相同类型的 bean,这将指示需要自动装配的 bean 名称。(以便 spring 可以按名称自动装配。)

@Primaryshould be used in conjuction with @Bean/ @Autowiredwhich 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 @Primaryis 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 @Qualifierdoesn'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 @Qualifierannotation to resolve an ambiguity. As @Qualifieris used at the injection point, there might be two situations where we don't want to or cannot use @Qualifier.

如果一个注入点有多个可用实例,那么我们可以使用@Qualifier注释来解决歧义。正如@Qualifier在注入点使用的那样,可能有两种情况我们不想或不能使用@Qualifier.

  1. When autowiring mode is Autowire.BY_TYPE. Then, of course, we cannot use @Qualifierbecause we actually don't have user-defined injection point specified as @Autowiredor @Inject
  2. We want to do bean selection (i.e. resolve the ambiguity) at configuration time rather than during beans development time.
  1. 当自动装配模式为Autowire.BY_TYPE. 那么,当然,我们不能使用,@Qualifier因为我们实际上没有将用户定义的注入点指定为@Autowired@Inject
  2. 我们希望在配置时而不是在 bean 开发期间进行 bean 选择(即解决歧义)。

The solution to above problems is to use @Primaryannotation.

上述问题的解决方案是使用@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,它将是自动装配的值。