Spring 5.0.8.RELEASE文档 Core1-1.3.1 命名 beans

版权声明:mj1001001 https://blog.csdn.net/qq_42786889/article/details/81711773

Every bean has one or more identifiers. These identifiers must be unique within the container that hosts the bean. A bean usually has only one identifier, but if it requires more than one, the extra ones can be considered aliases.

任何bean都有一个或者多个id。这些id在(承载的容器)容器中必须是唯一的。一个bean通常都只有一个id,但如果需要多个,额外的id会被认为是别名(name属性就是别名,也就是alias,不过也需要唯一)。

In XML-based configuration metadata, you use the id and/or name attributes to specify the bean identifier(s). The id attribute allows you to specify exactly one id. Conventionally these names are alphanumeric (‘myBean’, ‘fooService’, etc.), but may contain special characters as well. If you want to introduce other aliases to the bean, you can also specify them in the name attribute, separated by a comma (,), semicolon (;), or white space. As a historical note, in versions prior to Spring 3.1, the id attribute was defined as an xsd:ID type, which constrained possible characters. As of 3.1, it is defined as an xsd:string type. Note that bean id uniqueness is still enforced by the container, though no longer by XML parsers.

在xml配置文件中,你可以使用id或者name去标识bean。id属性让你指定唯一的id。通常name属性是字母字符串,例如(’myBean’,’fooService’)也可以包含特殊字符。如果你想要给bean设置名称,就可以使用name属性,如果多个name属性表示同一个bean可以用(,)、(;)或者空格分割。Spring 3.1之前的版本id属性的xml约束是xsd:ID,因此会限制了一些字符串,而Spring 3.1以后的版本xml约束换成了xsd:string,则可以是任何字符串。要记住bean的id一定是唯一的,不管你用没用xml配置还是代码配置。

You are not required to supply a name or id for a bean. If no name or id is supplied explicitly, the container generates a unique name for that bean. However, if you want to refer to that bean by name, through the use of the ref element or Service Locator style lookup, you must provide a name. Motivations for not supplying a name are related to using inner beans and autowiring collaborators.

如果你没给bean设置id或者name属性,容器会为bean自动生成唯一的name。但是这样的话,如果你想要利用name来引用这个bean,你就可能不知道容器生成的name是什么了,所以如果你需要这个bean作为合作类,就应该为它指定name或id来进行唯一标识。

Bean Naming Conventions
The convention is to use the standard Java convention for instance field names when naming beans. That is, bean names start with a lowercase letter, and are camel-cased from then on. Examples of such names would be (without quotes) ‘accountManager’, ‘accountService’, ‘userDao’, ‘loginController’, and so forth.

Bean的命名惯例
这里的管理是标准的Java实例化命名惯例。那就是bean的命名是驼峰法,首字母是小写。例如‘accountManager’,‘accountService’,‘loginController’等等。

Naming beans consistently makes your configuration easier to read and understand, and if you are using Spring AOP it helps a lot when applying advice to a set of beans related by name.

bean命名统一能让你的配置简单易读,如果你使用Spring AOP的话它能有效于一组bean关联名称。(没用过AOP应该就是指分组方便吧-,-)

With component scanning in the classpath, Spring generates bean names for unnamed components, following the rules above: essentially, taking the simple class name and turning its initial character to lower-case. However, in the (unusual) special case when there is more than one character and both the first and second characters are upper case, the original casing gets preserved. These are the same rules as defined by java.beans.Introspector.decapitalize (which Spring is using here).

提示:如果使用了classpath组件扫描,Spring 会自动为未命名的bean的根据上述的方法生成name:本质上是取出类名将首字母改成小写而已。但是总有些煞笔(特例)类名第一第二个字母都是大写,这时这两个大写会被保留。这些规则是java.beans.Introspector.decapitalize里面定义的。

Aliasing a bean outside the bean definition
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.

不在bean标签内定义别名,而在外部
在bean定义的时候,你可以为bean定义一个或多个名称,通过用id属性来指定唯一名称,name属性来指定多个名称这样组合。这些name等价于alias别名,在某些场景下特别有用。例如多个组件一起引用一个改了昵称的组件?提高可读性?-,-

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中(bean标签)定义的name不足以应付日常的(不足够),有时需要在其它地方来定义别名,例如一个大型的系统配置的分开,子系统会根据自己的命名规则去重新定义其别名,在xml配置文件中就是使用 标签来完成。

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

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

在当前例子中,一个bean原来在容器中叫fromName,定义了别名toName后我们也能用toName去引用它。

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

例如,配置中A子系统引用的DataSource叫subsystemA-dataSource,在B子系统中引用的叫subsystemB-dataSource。当两个子系统集成进一个主系统中,所引用的DataSource 叫myApp-dataSourc。这就出现了同一个DataSource对象有了三个不同的配置名称,能通过别名解决此命名问题:(同一个数据源对象被三个名称引用指向,有点像Java对象引用)

<alias name="subsystemA-dataSource" alias="subsystemB-dataSource"/>
<alias name="subsystemA-dataSource" alias="myApp-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.

现在每一个组件和主应用都能通过唯一的name引用dataSource,而且(保证)不用担心标识冲突或语义上的不美观(有效地创建命名空间),现在还是引用同一个bean。

Java-configuration
If you are using Java-configuration, the @Bean annotation can be used to provide aliases see Using the @Bean annotation for details.

Java代码配置
如果你使用Java代码配置的方式,@Bean注解有提供aliases别名的能力。查看1.12.3章节。

猜你喜欢

转载自blog.csdn.net/qq_42786889/article/details/81711773