Spring bean别名用法 [英] Spring bean alias usage

查看:290
本文介绍了Spring bean别名用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道bean的别名在春天意味着什么。但我想知道使用别名的用例。为什么有人想使用别名而不是名字来引用bean?

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?

提前致谢。

推荐答案

我见过的用法如下所示:您有两个给定接口的实例( SomeBean ):一个用于环境A,另一个用于环境B.因此,您定义了两个bean:一个名为someBeanForA ,另一个名为someBeanForB。

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的bean不知道必须使用哪一个:它取决于环境。所以他们使用别名:

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:

@Autowired
@Qualifier("someBeanAlias")
private SomeBean someBean;

部署到环境A时,XML文件中的别名指向someBeanA。部署到环境B时,XML文件中的别名指向someBeanB。

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.

这篇关于Spring bean别名用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆