Bean overview

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

PropertyExplained in…​
classSection 3.3.2, “Instantiating beans”
nameSection 3.3.1, “Naming beans”
scopeSection 3.5, “Bean scopes”
constructor argumentsSection 3.4.1, “Dependency Injection”
propertiesSection 3.4.1, “Dependency Injection”
autowiring modeSection 3.4.5, “Autowiring collaborators”
lazy-initialization modeSection 3.4.4, “Lazy-initialized beans”
initialization methodthe section called “Initialization callbacks”
destruction methodthe 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容器并发访问异常,以及状态不一致。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值