Spring Framework Documentation (5.3.10)
Core | IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP. |
1. The IoC Container
1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)
1.2. Container Overview (容器概览)
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 。