Spring Framework学习之路(8)--- 命名beans

本文介绍了Spring容器中Bean的命名规则,如何使用id和name属性指定标识符,以及bean命名的惯例。重点讲解了别名的使用场景,包括在定义外部和内部bean时的策略,以及Java配置中的@Bean注解。
摘要由CSDN通过智能技术生成

1.3.1. Naming beans

1.3.1. 命名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 attribute, the name attribute, or both to specify the bean identifiers. The id attribute lets you specify exactly one id. Conventionally, these names are alphanumeric (‘myBean’, ‘someService’, etc.), but they can contain special characters as well. If you want to introduce other aliases for 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。通常,这些名称是字母数字(“ myBean”,“ someService”等),但它们也可以包含特殊字符。如果要为bean引入其他别名,还可以在name属性中指定它们,并用逗号(,),分号(;)或空格分隔。作为历史记录,在Spring 3.1之前的版本中,id属性定义为xsd:ID类型,该类型限制了可能的字符。从3.1开始,它被定义为xsd:string类型。请注意,Bean ID唯一性仍由容器强制执行,尽管XML解析器不再如此。

  You are not required to supply a name or an id for a bean. If you do not supply a name or id 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 a 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。如果未明确提供名称或ID,则容器将为该bean生成一个唯一的名称。但是,如果要通过名称引用该bean,则必须通过使用ref元素或服务定位器样式查找,您必须提供一个名称。不提供名称的动机与使用内部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 there. Examples of such names include accountManager, accountService, userDao, loginController, and so forth.
  约定是在命名bean时将标准Java约定用于实例字段名称。 也就是说,bean名称以小写字母开头,并从那里用驼峰式大小写。 此类名称的示例包括accountManager,accountService,userDao,loginController等。

  Naming beans consistently makes your configuration easier to read and understand. Also, if you use Spring AOP, it helps a lot when applying advice to a set of beans related by name.
  一致地命名Bean使您的配置更易于阅读和理解。 另外,如果您使用Spring AOP,则在将建议应用于名称相关的一组bean时,它会很有帮助。

  With component scanning in the classpath, Spring generates bean names for unnamed components, following the rules described earlier: 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 uses here).
  通过在类路径中进行组件扫描,Spring会按照前面描述的规则为未命名的组件生成Bean名称:从本质上讲,采用简单的类名称并将其初始字符转换为小写。 但是,在(不寻常的)特殊情况下,如果有多个字符并且第一个和第二个字符均为大写字母,则会保留原始大小写。 这些规则与java.beans.Introspector.decapitalize(Spring在此使用)定义的规则相同。

Aliasing 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 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定义本身中,可以使用由id属性指定的一个名称和name属性中任意数量的其他名称的组合来为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 element to accomplish this. The following example shows how to do so:
  但是,在实际定义bean的地方指定所有别名并不总是足够的。 有时需要为在别处定义的bean引入别名。 这在大型系统中通常是这种情况,在大型系统中,配置在每个子系统之间分配,每个子系统都有自己的对象定义集。 在基于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.
  在这种情况下,在使用该别名定义之后,也可以将名为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的配置元数据可以通过子系统A-dataSource的名称引用数据源。 子系统B的配置元数据可以通过子系统B-dataSource的名称引用数据源。 组成使用这两个子系统的主应用程序时,主应用程序通过myApp-dataSource的名称引用DataSource。 要使所有三个名称都引用相同的对象,可以将以下别名定义添加到配置元数据中:

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

Java-configuration
Java的配置
  If you are using Java-configuration, the @Bean annotation can be used to provide aliases see Using the @Bean annotation for details.
  如果您正在使用Java配置,则可以使用**@Bean注释来提供别名,请参阅使用@Bean注释**以获取详细信息。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

工地码哥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值