Sping-framwork5-Core Technologies系列

(上一篇内容篇幅过多,阅读理解起来比较费力,今后细分小节,形成小篇幅阅读)

1.3 Bean Overview (Bean概述)BeanDefinition

A Spring IoC container manages one or more beans. These beans are created with the configuration metadata that you supply to the container (for example, in the form of XML definitions).

springioc容器管理一个或多个bean。这些bean是使用您提供给容器的配置元数据创建的(例如,以XML定义的形式)。

Within the container itself, these bean definitions are represented as BeanDefinition objects, which contain (among other information) the following metadata:

在容器本身中,这些bean定义表示为BeanDefinition对象,其中包含(除其他信息外)以下元数据:

  • A package-qualified class name: typically, the actual implementation class of the bean being defined.

  • 包限定类名:通常是定义的bean的实际实现类。

  • Bean behavioral configuration elements, which state how the bean should behave in the container (scope, lifecycle callbacks, and so forth).

  • Bean行为配置元素,说明Bean在容器中的行为(范围(作用域)、生命周期回调等等)。

  • References to other beans that are needed for the bean to do its work. These references are also called collaborators or dependencies.

  • 对bean执行其工作所需的其他bean的引用。这些引用也称为协作者或依赖项。

  • Other configuration settings to set in the newly created object — for example, the size limit of the pool or the number of connections to use in a bean that manages a connection pool.

  • 在新创建的对象中设置的其他配置设置,例如,池的大小限制或要在管理连接池的bean中使用的连接数。

This metadata translates to a set of properties that make up each bean definition. The following table describes these properties:

此元数据转换为组成每个bean定义的一组属性。下表描述了这些属性:

Property Explained in…
Class Instantiating Beans
Name Naming Beans
Scope Bean Scopes
Constructor arguments Dependency Injection
Properties Dependency Injection
Autowiring mode Autowiring Collaborators
Lazy initialization mode Lazy-initialized Beans
Initialization method Initialization Callbacks
Destruction method Destruction Callbacks

In addition to bean definitions that contain information on how to create a specific bean, the ApplicationContext implementations also permit the registration of existing objects that are created outside the container (by users). This is done by accessing the ApplicationContext’s BeanFactory through the getBeanFactory() method, which returns the BeanFactory DefaultListableBeanFactory implementation. DefaultListableBeanFactory supports this registration through the registerSingleton(..) and registerBeanDefinition(..) methods. However, typical applications work solely with beans defined through regular bean definition metadata.

除了包含有关如何创建特定bean的信息的bean定义之外,ApplicationContext实现还允许注册在容器外部(由用户创建)的现有对象。这是通过getBeanFactory()方法访问ApplicationContextBeanFactory来完成的,该方法返回BeanFactory DefaultListableBeanFactory实现。DefaultListableBeanFactory通过registerSingleton(..)registerBeanDefinition(..)方法支持此注册。然而,典型的应用程序只处理通过常规bean定义元数据定义的bean。

Bean metadata and manually supplied singleton instances need to be registered as early as possible, in order for the container to properly reason about them during autowiring and other introspection steps. While overriding existing metadata and existing singleton instances is supported to some degree, the registration of new beans at runtime (concurrently with live access to the factory) is not officially supported and may lead to concurrent access exceptions, inconsistent state in the bean container, or both.
Bean元数据和手动提供的单例实例需要尽早注册,以便容器在自动连接和其他内省步骤中正确地对它们进行推理。虽然在一定程度上支持重写现有元数据和现有的单例实例,但在运行时注册新的bean(同时对工厂进行实时访问)不受官方支持,并且可能导致并发访问异常、bean容器中的状态不一致,或两者兼而有之。
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都有一个或多个标识符。这些标识符在承载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属性中指定它们,用逗号()、分号()或空格隔开。作为历史记录,在spring3.1之前的版本中,id属性被定义为xsd:ID类型,约束了可能的字符。从3.1开始,它被定义为xsd:string类型。注意,beanid的唯一性仍然由容器强制执行,尽管不再由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提供nameid。如果不显式地提供nameid,容器将为该bean生成一个唯一的名称。但是,如果希望通过使用ref元素或服务定位器样式的查找按名称引用该bean,则必须提供名称。不提供名称的动机与使用inner beansautowiring自动连接协作器有关。

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(别名)

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 letting each component in an application 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的配置元数据中,您可以使用元素来实现这一点。下面的示例演示如何执行此操作:


In this case, a bean (in the same container) 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的配置元数据可以通过subsystemA-dataSource的名称引用数据源。子系统B的元数据可以通过subsystemB-dataSource的名称引用。当组合使用这两个子系统的主应用程序时,主应用程序通过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 use Javaconfiguration, the @Bean annotation can be used to provide aliases. See Using the @Bean Annotation for details.

如果使用Javaconfiguration@Bean注释可以用来提供别名。有关详细信息,请参阅使用@Bean注释。

你可能感兴趣的:(Sping-framwork5-Core Technologies系列)