Spring Bean概述 1.3. Bean overview
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.
spring ioc 容器管理者多个bean对象,这些对象根据你提供的配置元数据生成,例如 xml形式配置的元数据中标签的定义
Within the container itself, these bean definitions are represented as
BeanDefinition
objects, which contain (among other information) the following metadata:
在容器内部,有一个BeanDefinition
对象代表着这些bean的定义,它其中包含了如下的元数据信息:
- A package-qualified class name: typically the actual implementation
class of the bean being defined.
代表这个类定义的具体实现类的全类名
- 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的引用,这些引用也被称为合作者或者依赖关系
Other configuration settings to set in the newly created object, for example, the number of connections to use in a bean that manages a connection pool, or the size limit of the pool.
还有一些别的配置,例如在每个bean中使用的连接池中连接的数量或者连接池大小的限制
This metadata translates to a set of properties that make up each bean definition.
以下的这些元数据构成了组成每个bean定义的属性集合
Table 1. The bean definition
属性 | 简单解释 |
---|---|
class | 这个类的全类名 |
name | 代表这个类的别名 |
scope | 可以理解为bean的作用范围 类似单例模式、原型模式 |
constructor arguments | 构造器参数 |
properties | 属性依赖 |
autowiring mode | 自动装配模式 |
lazy-initialization mode | 懒加载模式 |
initialization method | 初始化的方法 |
destruction method | 销毁方法 |
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 via the method getBeanFactory() which returns the BeanFactory implementation DefaultListableBeanFactory. DefaultListableBeanFactory supports this registration through the methods registerSingleton(..) and registerBeanDefinition(..). However, typical applications work solely with beans defined through metadata bean definitions.
除了包含有关如何创建一个特定的bean的定义之外,ApplicationContext的实现类还允许用户注册在spring 容器外创建的对象。这是通过访问ApplicationContext通过getBeanFactory()方法获取的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 of existing metadata and existing singleton instances is supported to some degree, the registration of new beans at runtime (concurrently with live access to factory) is not officially supported and may lead to concurrent access exceptions and/or inconsistent state in the bean container.
Bean定义的和手动提供的单例实例约早注册越好,这是为了能让容器更好的在自动装配和其他的自检操作步骤中理解他们。尽管在一定程度上支持现有元数据和单例实例的覆盖,但是在运行时注册新的bean对象(同时实时访问工厂)并没有得到官方的支持,并且这可能导致bean容器中的并发访问异常或不一致的状态;