Spring Bean的命名 1.3.1. Naming beans
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有一个或多个身份标识。这些标识在装载这些bean的容器中必须是唯一的。一个bean通常只有一个身份标识,但是如果它还需要更多的身份标识,那么可以通过别名机制来实现。
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属性只允许你指定一个。按照惯例,这些标识都是字母,例如myBean或fooService等,但是也有可能包含特殊符号。如果你想给bean取别名,你可以使用name属性来指定多个别名,别名之间可以用,或;或空白符号隔开。作为一个历史记录,在spring3.1版本中,id属性必须使用xsd定义的类型,这造成了一些限制。在spring3.1中,它被定义为xsd的string类型。请注意,虽然在xml解析器中不再强制要求id属性的唯一性,但是在spring容器中,它还是被强制要求的。
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属性。如果没有明确地指明id或name属性,容器会自动给这个bean生成一个唯一的name。但是,如果你想通过name属性引用这个bean用于ref的元素或服务定位器的查找,则你必须提供一个name属性。不提供name属性的操作通常与内部bean和自动装配有关。
Bean Naming Conventions Bean的命名规范
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的name以小写字母开头,并采用驼峰命名规则。例如accountManager、accountService、userDao、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配置切面的时候应用带有name的bean会给你带来很大的好处。
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).
使用类路径中的组件扫描,spring会为未命名的组件生成对应的name,具体的生成规则如下:使用这个bean类的类名,并且将类名的首字母小写的命名规则来创建。但是,有一些不常见的情况,当第一个字符和第二个字符都是大写字母的时候,会保留原始的命名。这和java.beans.Introspector.decapitalize
中的定义相同,这也正是spring使用的规则。
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别名来表示这个bean,并且name属性中的别名数量是不定的。这个names与这个bean的别名是同等的。在某些情况下特别有用,例如允许一个应用中的每个组件都能通过相关的bean的name属性的值来引用这些组件。
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.
<alias name="fromName" alias="toName"/>
一下子指定bean的所有别名是不现实的,有时候需要为在别的地方定义的bean起一个别名。在大型的系统中,在每个子系统分配配置的时候,每个子系统都有他们自己的一套对象的定义规则,在基于xml配置的元数据中米可以使用alias标签来实现。讲的通俗一点,就是我有一套总的父配置,现在在我的子配置中我可以给这写配置起个自己的别名便于管理。
<alias name="fromName" alias="toName"/>
经过以上的配置之后,在同一个容器中,name为fromName的bean就拥有了一个新的别名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:
<alias name="subsystemA-dataSource" alias="subsystemB-dataSource"/>
<alias name="subsystemA-dataSource" alias="myApp-dataSource" />
举个例子,子系统A可能通过subsystemA-dataSource来引用一个DataSource.子系统B可能通过subsystemB-dataSource来引用一个DataSource.在整合这两个子系统的时候是通过myApp-dataSource来获取一个数据源。这三个name都引用同一个对象。
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,这就是外部别名的好处
If you are using Java-configuration, the @Bean annotation can be used to provide aliases see Using the @Bean annotation for details.
如果你使用Java注解来进行配置,你可以使用Bean注解来进行别名的设置