Naming beans

3.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通常只有一个标识,不是如果需要多个标识的化,多余的会被认为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 属性允许你确定的指定恰当的一个id,通常这些名字 是数据与字母的组合(’myBean’, ‘fooService’, 等),但是也可以包含页数符号,你如过你想给bean 添加别名,只要在name属性中添加使用(,)半角逗号,(;),或者空格分隔。历史版中中,Spring3.1 之前的版本,id属性被定义为 xsd:ID 类型。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提供一个name或者id。在没有明确指定name或者id的时候,容器会给这些bean生成一个唯一的name。然后,当你想通过ref节点或者 服务定位查找,去引用这些bean的时候,你必须提供一个name属性。不提供name的bean 与使用内部bean以及自动注入有关。

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.

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.


使用Java命名实例文件的标准的规范来命名beans,也就是说,bean命名以小写字母开始,然后采用驼峰命名方法,例如这样的名字(不带引号) ‘accountManager’, ‘accountService’, ‘userDao’, ‘loginController’等等。

命名bean能够让你的配置 更容易读取以及理解。如果你使用Spring AOP时候,使用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).


在使用组件扫描classpath的时候,Spring 会根据以上规则对未命名的bean自动生成name,本质上就是,简单使用类的名称并将首字母转换为小写。但是,在特殊(异常)情况下,有多个字符,对一个以及第二个字符都是大小的化,将使用原来的名字。这些与java.beans.Introspector.decapitalize (Spring 使用的)的定义的规则是一样的。


Aliasing a bean outside the bean definition(给外部定义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 加上使用name 属性定义的多个name组合起来,你可以对一个bean 定义多个name,这个名字是一个bean的等效别名,应用在一些场景用,例如:应用的每个组件可以特定与半身的beanname 来引用常见的依赖。


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 引入别名,在大型系统中是常见的。配置分配在每一个子系统中,并且每个子系统有自己的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.

—这种情况下,一个容器中的一个叫做 froName的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:


例如,子系统A 的配置元数据通过 name subsystemA-dataSource 引用 Datasource,子系统B 的配置元数据通过 name subsystemB-dataSource 引用 Datasource,当主应用集成了这两个子系统的时候 通过 myApp-dataSource 来引用DataSource,为了让者三个名字引用同一个对象,以需要咋 MaApp的配置元数据中天下下面的别名定义。

<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.


现在 每个组件以和主应用程序都可以使用为一个 名称来引用 dataSource,并且保证不会与其他一定冲突。因为它们引用的是一个bean。


Java-configuration(JAVA 代码配置)


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

— 如果你使用的是 java配置, @bean 注解可是用来 提供别名, 详情查看3.12.3 “使用@bean注解”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'instanceOperatorClientImpl' defined in URL [jar:file:/E:/service/nacos-server-2.2.3/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-naming-2.2.3.jar!/com/alibaba/nacos/naming/core/InstanceOperatorClientImpl.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientOperationServiceProxy' defined in URL [jar:file:/E:/service/nacos-server-2.2.3/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-naming-2.2.3.jar!/com/alibaba/nacos/naming/core/v2/service/ClientOperationServiceProxy.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'persistentClientOperationServiceImpl' defined in URL [jar:file:/E:/service/nacos-server-2.2.3/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-naming-2.2.3.jar!/com/alibaba/nacos/naming/core/v2/service/impl/PersistentClientOperationServiceImpl.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.naming.core.v2.service.impl.PersistentClientOperationServiceImpl]: Constructor threw exception; nested exception is java.lang.UnsatisfiedLinkError: C:\Users\Administrator\AppData\Local\Temp\librocksdbjni15037951375079649142.dll:
最新发布
07-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值