Spring - 别名与名称的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15211590/
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 - usage of alias vs names
提问by yapkm01
I am confused on the usage of alias. I do understand what alias is and how it is being used but i don't see how it can be any different than using names on a bean definition.
我对别名的使用感到困惑。我确实了解别名是什么以及它是如何使用的,但我看不出它与在 bean 定义中使用名称有何不同。
<bean id="xyx" name="abc,def" .. />
<alias name="xyx" alias="pqr"/>
Why the alias when i can use abc or def?
当我可以使用 abc 或 def 时,为什么要使用别名?
采纳答案by Marcin Wasiluk
In my mind bean aliasing can be helpful in large system, where you can not manipulate bean names. You have option to create your own name (alias) specific for your part of the system...
在我看来,bean 别名在无法操作 bean 名称的大型系统中很有用。您可以选择为您的系统部分创建自己的名称(别名)...
from Spring documentation (3.0.x) http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/
来自 Spring 文档 (3.0.x) http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/
...it is sometimes desirable to give a single bean multiple names, otherwise known as bean aliasing...
...有时需要给单个 bean 多个名称,也称为 bean 别名 ...
therefore creating multiple names or/and aliasing are the same thing.
因此创建多个名称或/和别名是一回事。
回答by Adil
A use case maybe when you want to customize some beans that are already defined somewhere in a modular application (each module is a spring project for example), the bean maybe defined by a third-party framework/API or even your team. In that case you want that only inside your spring project call the customized version without altering other modules (projects), to do that just add the alias in your spring configuration which is indeed a powerful feature:
一个用例可能是当您想自定义一些已经在模块化应用程序中某个地方定义的 bean(例如,每个模块都是一个 spring 项目)时,bean 可能由第三方框架/API 甚至您的团队定义。在这种情况下,您希望仅在您的 spring 项目中调用自定义版本而不更改其他模块(项目),为此只需在您的 spring 配置中添加别名,这确实是一个强大的功能:
<alias alias="globalBeanService" name="customizedBeanService" />
Hence, whenever spring find a call to the globalBeanService, it will inject customizedBeanService for you inside your specific module. Without this feature, you should go through all classes and modify the bean manually!!
因此,每当 spring 找到对 globalBeanService 的调用时,它都会在您的特定模块中为您注入定制的BeanService。如果没有这个特性,你应该遍历所有类并手动修改 bean !
回答by Yasser Afifi
An aliased bean will always have higher priority over a non-aliased one, and in case of having different beans with the same alias then the last one declared will have the priority. In other words, the aliased bean will override the non-aliased beans. This can be particularly useful when creating big projects or when you are building extensions to your project and don't want to touch the original bean definition.
别名 bean 总是比非别名 bean 具有更高的优先级,并且如果具有相同别名的不同 bean,那么最后一个声明的 bean 将具有优先级。换句话说,别名 bean 将覆盖非别名 bean。这在创建大型项目或为项目构建扩展并且不想触及原始 bean 定义时特别有用。
回答by John
Alias has a specific using scenario which multiple names don't have:
Alias 具有多个名称没有的特定使用场景:
Imagine multiple config xml files in your project, most of which are authored by your colleagues, and you need to add your own config.xml file. Using you'll be able to refer to a bean defined in another config file with a different name that's maybe more meaningful to your config, without having to touch your colleagues' config files.
想象一下你的项目中有多个 config xml 文件,其中大部分是你的同事编写的,你需要添加自己的 config.xml 文件。使用您将能够引用在另一个配置文件中定义的 bean,该 bean 具有不同的名称,这可能对您的配置更有意义,而无需接触您同事的配置文件。
回答by kap
I recently found another use case where alias easily solved a problem.
我最近发现了另一个用例,别名很容易解决了一个问题。
When auto configuration is active, Spring Boot provides the bean serverPropertieswhich can be used to access information about the server currently running the web app.
当自动配置处于活动状态时,Spring Boot 提供serverProperties可用于访问有关当前运行 Web 应用程序的服务器的信息的 bean 。
In integration tests (i.e. when @SpringBootTestannotation is present) the same bean is available under the name org.springframework.boot.autoconfigure.web.ServerProperties.
在集成测试中(即当@SpringBootTest存在注解时)相同的 bean 在名称下可用org.springframework.boot.autoconfigure.web.ServerProperties。
Of course it is possible to use a different profile for integration testing, but that would require manual change of configuration at multiple places. However, simply by adding
当然,可以使用不同的配置文件进行集成测试,但这需要在多个位置手动更改配置。但是,只需添加
<alias name="serverProperties" alias="org.springframework.boot.autoconfigure.web.ServerProperties"/>
the same configuration files can be used for integration tests and in production.
相同的配置文件可用于集成测试和生产。
This might be a bug in Spring Boot, however alias easily solve the problem without waiting for a new release. And most certainly I have no possibility to alter the Boot configuration myself.
这可能是 Spring Boot 中的一个错误,但是别名可以轻松解决问题,而无需等待新版本。最肯定的是,我不可能自己更改引导配置。

