spring @Autowire 默认模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5665220/
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
@Autowire default mode
提问by Pomario
How does Spring @Autowirebeans: byName or byType? If one is not possible, is a second trial done using another mode?
Spring @Autowirebean:byName 或 byType怎么做?如果不可能,是否使用另一种模式进行第二次试验?
回答by Wilhelm Kleu
If annotated with @Autowiredit will inject the bean with the matching type (An exception will be thrown if there are more than one of a type). To specify a name use the @Qualifierannotation.
如果用@Autowired它注释将注入具有匹配类型的 bean(如果有多个类型,将抛出异常)。要指定名称,请使用@Qualifier注释。
回答by Daff
Springs @Autowirewires by type. For wiring by name you can also use
弹簧@Autowire线按类型。对于按名称接线,您还可以使用
@Resource(name = "id")
回答by YJiao
The default mode of the @Autowiredis byType.
的默认模式@Autowired是byType。
回答by sendon1982
Autowiredannotation on variable or setters method is equivalent to xml attribute autowire="byType"
Autowired变量或 setters 方法上的注释等同于 xml 属性 autowire="byType"
XML attribute autowireis defaut as no
XML 属性autowire默认为no
"no":
The traditional Spring default. No automagical wiring. Bean references
must be defined in the XML file via the <ref/> element (or "ref"
attribute). We recommend this in most cases as it makes documentation
more explicit.
回答by harit
These are no, byName, byType, constructor, and autodetect. The default mode is no i.e. by default autowiring is turned off in traditional XML based configuration.
它们是 no、byName、byType、构造函数和自动检测。默认模式为 no,即默认情况下自动装配在传统的基于 XML 的配置中是关闭的。
Using @Autowired annotation-
使用@Autowired 注释-
1) @Autowired on properties:
1)@Autowired 在属性上:
When @Autowired is used on properties, it is equivalent to autowiring by byType in configuration file.
在属性上使用@Autowired 时,相当于通过配置文件中的byType 自动装配。
2) @Autowired on property setters:
2) @Autowired 在属性设置器上:
When @Autowired is used on setters, it is also equivalent to autowiring by byType in configuration file.
当@Autowired 用于setter 时,也相当于通过配置文件中的byType 自动装配。
3) @Autowired on constructors:
3) @Autowired 在构造函数上:
When @Autowired is used on bean's constructor, it is also equivalent to autowiring by constructor in configuration file.
在 bean 的构造函数上使用@Autowired 时,也相当于在配置文件中通过构造函数进行自动装配。
Use @Qualifier for conflict in dependency resolution
使用@Qualifier 解决依赖项冲突
As we learned that if we are using autowiring in byType mode and dependencies are looked for property class types. If no such type is found, an error is thrown. But, what if there are two or more beans for same class type.
正如我们所了解到的,如果我们在 byType 模式下使用自动装配,并且依赖项会查找属性类类型。如果没有找到这样的类型,则会抛出错误。但是,如果同一类类型有两个或多个 bean 呢?
In this case spring will not be able to choose correct bean to inject into property, and you will need to help the container using qualifiers.
在这种情况下,spring 将无法选择正确的 bean 注入到属性中,您将需要使用限定符帮助容器。
To resolve a specific bean using qualifier, we need to use @Qualifier annotation along with @Autowired annotation and pass the bean name in annotation parameter.
要使用限定符解析特定的 bean,我们需要使用 @Qualifier 批注和 @Autowired 批注,并在批注参数中传递 bean 名称。

