3.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 容器管理一个或者多个beans,这些beans 依据你提供给容器的配置数据来创建,例如 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 behavioral configuration elements, which state how the bean should behave in the container (scope, lifecycle callbacks, and so forth).
References to other beans that are needed for the bean to do its work; these references are also called collaborators or dependencies.
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 的实现类。
- Bean的行为配置节点,展示bean在容易中的行为(作用域,生命周期,回掉)
- 为保证正常工作对其他bean的引用,也被成为依赖。
- 其他配置信息设置到新创建的对象中,,在管理连接池的bean中添加 链接数或者链接池的大小。
This metadata translates to a set of properties that make up each bean definition.
这些元数据转换成一个属性集合(Set)来完成每个bean的定义。
The bean definition
Property | Explained in… |
---|---|
class | Section 3.3.2, “Instantiating beans” |
name | Section 3.3.1, “Naming beans” |
scope | Section 3.5, “Bean scopes” |
constructor arguments | Section 3.4.1, “Dependency Injection” |
properties | Section 3.4.1, “Dependency Injection” |
autowiring mode | Section 3.4.5, “Autowiring collaborators” |
lazy-initialization mode | Section 3.4.4, “Lazy-initialized beans” |
initialization method | the section called “Initialization callbacks” |
destruction method | the section called “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 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的bean definitions之外, ApplicationContext 实现也允许用户将容器外创建的对象进行注册, 可以通过 getBeanFactory()得到BeanFactory的实现DefaultListableBeanFactory 来访问ApplicationContext的 BeanFactory。DefaultListableBeanFactory 支持两种注册Bean的方式分别是egisterSingleton(..) 和registerBeanDefinition(..)。但是,常规的应用只能通过使用元数据来定义beans
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元数据以及 手动提供的单利实例需要尽早的注册,以便于Ioc 容器 能够在激动装配以及 其他操作时 正确的处理它们。在某种情况下 重写 配置元数据 以及 单例实例时支持的,但是在运行时 注册新的beans(实时访问工厂时)是不支持的,可能导致 bean容器并发访问异常,以及状态不一致。