1.3.1.1Aliasing a Bean outside the Bean Definition 在Bean定义外指定Bean别名

  Spring Framework Documentation (5.3.10)

Core

IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP.

   Core Technologies

1. The IoC Container

1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)

1.2. Container Overview (容器概览)

1.3. Bean Overview (Bean概览)

1.3.1. Naming Beans (Bean命名)

1.3.1.1Aliasing a Bean outside the Bean Definition 在Bean定义外指定Bean别名

1.3.2. Instantiating Beans实例化bean

1.3.2.1.Instantiation with a Constructor (通过构造函数进行实例化)

1.3.2.2. Instantiation with a Static Factory Method (通过静态工厂方法进行实例化)

1.3.2.3. Instantiation by Using an Instance Factory Method (使用实例工厂方法进行实例化)

1.3.2.4. Determining a Bean’s Runtime Type(确定Bean的运行时类型)


下载此文档精编完整版

 No.内容下载地址文档内容目录
1中英双语精编版 第一部分PDF下载内容目录
2中英双语精编版 第二部分PDF下载内容目录
3中文精编版 第一部分PDF下载内容目录
4中文精编版 第二部分PDF下载内容目录

更多章节内容,请点击查看:  Core Technologies


1.3.1.1Aliasing a Bean outside the Bean Definition 在Bean定义外指定Bean别名

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 letting each component in an application refer to a common dependency by using a bean name that is specific to that component itself.

bean定义的name 属性中,最多指定一个由使用id属性指定的名称,以及任意多个其他名称的组合,从而可以为bean提供多个名称。这些名称可以是同一bean的等效别名,在某些情况下非常有用,例如,通过使用特定于某组件的bean名称,让应用程序中的每个组件引用公共依赖项。

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, with each subsystem having its own set of object definitions. In XML-based configuration metadata, you can use the <alias/> element to accomplish this. The following example shows how to do so:

但是,指定实际定义bean的所有别名并不总是足够的。有时需要为在其他地方定义的bean引入别名。在大型系统中,配置通常在每个子系统之间分开部署,每个子系统都有自己的对象定义集。在基于XML的配置元数据中,您可以使用<alias/>元素来实现这一点。以下示例展示了如何执行此操作:

<alias name="fromName" alias="toName"/>

In this case, a bean (in the same container) named fromName may also, after the use of this alias definition, be referred to as toName.

在这种情况下,使用此别名定义后,名为fromName bean(在同一容器中)也可以称为toName

For example, the configuration metadata for subsystem A may refer to a DataSource by the name of subsystemA-dataSource. The configuration metadata for subsystem B may refer to a DataSource by the name of subsystemB-dataSource. When composing the main application that uses both these subsystems, the main application refers to the DataSource by the name of myApp-dataSource. To have all three names refer to the same object, you can add the following alias definitions to the configuration metadata:

例如,子系统A的配置元数据可能以subsystemA-dataSource的名称引用数据源(DataSource)。子系统B的配置元数据可以通过subsystemB-dataSource的名称引用数据源。在编写使用这两个子系统的主应用程序时,主应用程序以myApp-dataSource的名称引用数据源。要使所有三个名称都引用同一对象,可以将以下别名定义添加到配置元数据中:

<alias name="myApp-dataSource" alias="subsystemA-dataSource"/>
<alias name="myApp-dataSource" alias="subsystemB-dataSource"/>

Now each component and the main application can refer to the dataSource through a name that is unique and guaranteed not to clash with any other definition (effectively creating a namespace), yet they refer to the same bean.

现在,每个组件和主应用程序都可以通过一个唯一的名称引用数据源,并保证不会与任何其他定义冲突(有效地创建命名空间),但它们引用的是同一个bean

Java-configuration

If you use Javaconfiguration, the @Bean annotation can be used to provide aliases. See Using the @Bean Annotation for details.

Java-configuration

如果使用Javaconfiguration@Bean注解可以用来提供别名。有关详细信息,请参见Using the @Bean Annotation 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月满闲庭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值