Rhyme/Spring Bean概述 1.3. Bean overview

本文介绍了 Spring IoC 容器中的 Bean 概念及其定义方式,包括 Bean 的基本属性如作用域、生命周期等,并阐述了 BeanDefinition 的内容及手动注册 Bean 的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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容器中的并发访问异常或不一致的状态;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值