在 Spring 中何时使用 <ref bean> 以及何时使用 <ref local>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8740322/
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
When to use <ref bean> and when to use <ref local> in Spring?
提问by Jyotirup
When to use <ref bean="service" />and when to use <ref local="service" />in Spring?
什么时候用<ref bean="service" />,什么时候用<ref local="service" />Spring?
回答by Aravind A
Specifying the target bean by using the bean attribute of the ref tag is the most general form, and will allow creating a reference to any bean in the same BeanFactory/ApplicationContext (whether or not in the same XML file), or parent BeanFactory/ApplicationContext. The value of the bean attribute may be the same as either the id attribute of the target bean, or one of the values in the name attribute of the target bean.
<ref bean="someBean"/>Specifying the target bean by using the local attribute leverages the ability of the XML parser to validate XML id references within the same file. The value of the local attribute must be the same as the id attribute of the target bean. The XML parser will issue an error if no matching element is found in the same file. As such, using the local variant is the best choice (in order to know about errors are early as possible) if the target bean is in the same XML file.
<ref local="someBean"/>
使用 ref 标记的 bean 属性指定目标 bean 是最通用的形式,并且将允许创建对同一 BeanFactory/ApplicationContext(无论是否在同一 XML 文件中)或父 BeanFactory/ApplicationContext 中的任何 bean 的引用. bean 属性的值可能与目标 bean 的 id 属性或目标 bean 的 name 属性中的值之一相同。
<ref bean="someBean"/>通过使用 local 属性指定目标 bean 可以利用 XML 解析器验证同一文件中的 XML id 引用的能力。local 属性的值必须与目标 bean 的 id 属性相同。如果在同一文件中找不到匹配的元素,XML 解析器将发出错误。因此,如果目标 bean 位于同一个 XML 文件中,则使用本地变体是最佳选择(以便尽早了解错误)。
<ref local="someBean"/>
This is from the Spring source reference here
这是来自此处的 Spring 源参考
回答by Vinod Cyriac
The local attribute on the ref element is no longer supported in the 4.0 beans xsd since it does not provide value over a regular bean reference anymore. Simply change your existing ref local references to ref bean when upgrading to the 4.0 schema.
4.0 beans xsd 不再支持 ref 元素上的 local 属性,因为它不再通过常规 bean 引用提供值。升级到 4.0 模式时,只需将现有的 ref 本地引用更改为 ref bean。
回答by rakehell
<ref local="someBeanId">should be used when you have a duplicate id in your parent-child config files and you want to distinguish between the two in either config file.<ref parent="someBeanId">should be used in the child config file to reference the parent id.<ref bean="someBeanId">should be used when you do not have a duplicate id in your parent-child config files.
<ref local="someBeanId">当您的父子配置文件中有重复的 id 并且您想在任一配置文件中区分两者时,应该使用。<ref parent="someBeanId">应该在子配置文件中使用以引用父 ID。<ref bean="someBeanId">当您的父子配置文件中没有重复的 id 时,应该使用。
回答by skaffman
<ref local="..">requires that the bean being referenced is in the same config file.
<ref local="..">要求被引用的 bean 在同一个配置文件中。
<ref bean="...">requires only it to be in the same context, or in a parent context.
<ref bean="...">只需要它在相同的上下文中,或者在父上下文中。
The difference is primarily one of documentation. If you see <ref local="...">, then you know you need only look in the same file to find it. Other than that, there's not much difference. I would generally use <ref bean="...">in most cases.
区别主要是文档之一。如果您看到<ref local="...">,那么您知道您只需要查看同一个文件即可找到它。除此之外,没有太大区别。我通常会<ref bean="...">在大多数情况下使用。
回答by dzgeek
In spring 4.1 the local attribute is not valid.
在 spring 4.1 中,本地属性无效。
i used in the parent xml a name attribute for the
我在父 xml 中使用了一个名称属性
and in the child file a referenced the bean by the alias i gave already.
在子文件中,我已经给定的别名引用了 bean。

