java Spring bean 别名用法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13220919/
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
Spring bean alias usage
提问by Newbie
I know what bean alias means in spring. But I want to know the use cases for making use of alias. Why would somebody want to refer a bean using alias name instead of its name?
我知道 bean 别名在春天意味着什么。但我想知道使用别名的用例。为什么有人要使用别名而不是名称来引用 bean?
Thanks in advance.
提前致谢。
采纳答案by JB Nizet
A usage I've seen is the following: you have two instances of a given interface (SomeBean
): one for environment A, and one for environment B. So you define two beans: one named "someBeanForA", and the other one named "someBeanForB".
我见过的一种用法如下:你有给定接口 ( SomeBean
) 的两个实例:一个用于环境 A,一个用于环境 B。所以你定义了两个 bean:一个名为“someBeanForA”,另一个名为“ someBeanForB”。
The beans where this SomeBean must be injected don't know which one they must use: it depends on the environment. So they use an alias:
必须注入这个 SomeBean 的 bean 不知道它们必须使用哪一个:这取决于环境。所以他们使用别名:
@Autowired
@Qualifier("someBeanAlias")
private SomeBean someBean;
When deploying to the environment A, the alias in the XML file points to someBeanA. When deploying to the environment B, the alias in the XML file points to someBeanB.
部署到环境A时,XML文件中的别名指向someBeanA。部署到环境B时,XML文件中的别名指向someBeanB。
回答by ElderMael
I think the reference documentationexplains it very well:
我认为参考文档很好地解释了它:
In a bean definition itself, you can supply more than one name for the bean, by using a combination of up to one name specified by the id attribute, and any number of other names in the name attribute. These names can be equivalent aliases to the same bean, and are useful for some situations, such as allowing each component in an application to refer to a common dependency by using a bean name that is specific to that component itself.
Specifying all aliases where the bean is actually defined is not always adequate, however. It is sometimes desirable to introduce an alias for a bean that is defined elsewhere. This is commonly the case in large systems where configuration is split amongst each subsystem, each subsystem having its own set of object definitions. In XML-based configuration metadata, you can use the element to accomplish this.
在 bean 定义本身中,您可以通过使用 id 属性指定的最多一个名称和 name 属性中任意数量的其他名称的组合,为 bean 提供多个名称。这些名称可以是同一个 bean 的等效别名,并且在某些情况下很有用,例如允许应用程序中的每个组件通过使用特定于该组件本身的 bean 名称来引用公共依赖项。
然而,在实际定义 bean 的地方指定所有别名并不总是足够的。有时需要为在别处定义的 bean 引入别名。这在大型系统中很常见,其中配置在每个子系统之间拆分,每个子系统都有自己的一组对象定义。在基于 XML 的配置元数据中,您可以使用元素来完成此操作。
An specific example may be where you must define an entry point for authentication in a Single Sign On module for multiple applications. You define it in a single Spring Bean definition and you alias it in your specific application to use it as an authentication entry point.
一个特定示例可能是您必须在单点登录模块中为多个应用程序定义身份验证入口点。您可以在单个 Spring Bean 定义中定义它,并在您的特定应用程序中为其添加别名以将其用作身份验证入口点。
回答by Arun Manivannan
There is an example in the springframework docsitself.
springframework 文档本身中有一个示例。
As a concrete example, consider the case where component A defines a DataSource bean called componentA-dataSource, in its XML fragment. Component B would however like to refer to the DataSource as componentB-dataSource in its XML fragment. And the main application, MyApp, defines its own XML fragment and assembles the final application context from all three fragments, and would like to refer to the DataSource as myApp-dataSource.
作为一个具体的例子,考虑组件 A 在其 XML 片段中定义一个名为 componentA-dataSource 的 DataSource bean 的情况。然而,组件 B 想在其 XML 片段中将数据源称为 componentB-dataSource。主应用程序 MyApp 定义了自己的 XML 片段,并从所有三个片段组装了最终的应用程序上下文,并希望将 DataSource 称为 myApp-dataSource。
回答by Tatha
We have used alias in our project and the reason why we used this is because
我们在我们的项目中使用了别名,我们使用它的原因是因为
For an use case, the architecture is such that the bean ids are mentioned in an database master table column. So when the flow is invoked, it reads the table and loads the bean with the same name provided in the column. The bean definition is present in an applicationContext which is part of a jar and we cant change that.
对于用例,架构是在数据库主表列中提及 bean id。因此,当流程被调用时,它读取表并加载列中提供的具有相同名称的 bean。bean 定义存在于 applicationContext 中,它是 jar 的一部分,我们无法更改它。
Now for some instances, we had to change the bean name in the table column(to provide better naming convention) but since we cant change the context bean definition, we used aliases to map the new name to the older bean id.
现在对于某些情况,我们必须更改表列中的 bean 名称(以提供更好的命名约定),但由于我们无法更改上下文 bean 定义,我们使用别名将新名称映射到旧 bean id。
This is also helps in the scenario of multiple environments. If aliases are present we don't have to run the script to update the column value in each environment.
这在多环境的场景中也有帮助。如果存在别名,我们就不必运行脚本来更新每个环境中的列值。