1.3.1. Naming Beans (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. Naming Beans (Bean命名)

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. However, if it requires more than one, the extra ones can be considered aliases.

每个bean都有一个或多个标识符(identifier)。这些标识符在承载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解析器(XML parser)强制检查。

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提供name id。如果没有显式地提供name id,容器将为该bean生成唯一的名称。但是,如果希望通过使用ref元素或服务定位器样式查找(Service Locator style lookup)按名称引用该bean,则必须提供名称。不提供名称的动机与使用内部bean inner beans)和自动装配协作器(autowiring collaborators)有关。

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 there. Examples of such names include accountManageraccountServiceuserDaologinController, and so forth.

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命名约定

在命名bean时,使用标准Java约定(standard Java convention)作为实例字段名。也就是说,bean名称以小写字母开头,然后用驼峰大小写。此类名称的示例包括accountManageraccountServiceuserDaologinController等。

命名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).

在对classpath中的组件扫描时,Spring为未命名的组件生成bean名称,并遵循前面描述的规则:本质上,采用简单的类名并将其首字母转换为小写。但是,在(不常见的)特殊情况下,当有多个字符且第一个和第二个字符都是大写时,原始大小写将被保留。这些规则与ava.beans.Introspector.decapitalize(在这里Spring使用)定义的规则相同。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

月满闲庭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值