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 <bean/>
definitions).
Within the container itself, these bean definitions are represented as BeanDefinition
objects, which contain (among other information) the following metadata:
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:
以下是 BeanDefinition 包含的属性值
Table 1. The bean definition
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.
Note
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.