1.3. Bean Overview (Bean概览)

 Spring Framework Documentation (5.3.10)

Core

IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP.

   Core Technologies

1. The IoC Container

1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)

1.2. Container Overview (容器概览)

1.3. Bean Overview (Bean概览)

1.3.1. Naming Beans (Bean命名)

1.3.1.1Aliasing a Bean outside the Bean Definition 在Bean定义外指定Bean别名

1.3.2. Instantiating Beans实例化bean

1.3.2.1.Instantiation with a Constructor (通过构造函数进行实例化)

1.3.2.2. Instantiation with a Static Factory Method (通过静态工厂方法进行实例化)

1.3.2.3. Instantiation by Using an Instance Factory Method (使用实例工厂方法进行实例化)

1.3.2.4. Determining a Bean’s Runtime Type(确定Bean的运行时类型)


下载此文档精编完整版

 No.内容下载地址文档内容目录
1中英双语精编版 第一部分PDF下载内容目录
2中英双语精编版 第二部分PDF下载内容目录
3中文精编版 第一部分PDF下载内容目录
4中文精编版 第二部分PDF下载内容目录

更多章节内容,请点击查看:  Core Technologies


1.3. Bean Overview (Bean概览)

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).

Spring IoC容器管理一个或多个bean。这些bean是使用您提供给容器的配置元数据创建的(例如,以XML<bean/>定义的形式)。

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 size limit of the pool or the number of connections to use in a bean that manages a connection pool.

This metadata translates to a set of properties that make up each bean definition. The following table describes these properties:

  • 包限定类名(package-qualified class name):通常是定义的bean的实际实现类。
  • Bean行为配置元素(behavioral configuration element),用于说明bean在容器中的行为(作用域(scope)、生命周期回调等)。
  • Bean正常工作所需的其他bean的引用。这些引用也称为协作者或依赖项(collaborators or dependencies)。
  • 在新创建对象中设置的其他配置设置,例如,池的大小限制,或管理连接池的bean中要使用的连接数。

此元数据转换为组成每个bean定义的一组属性。下表介绍了这些属性:

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.

除了包含关于如何创建特定bean的信息的bean定义外,ApplicationContext 实现还允许(由用户)注册在容器外部创建的现有对象。这是通过getBeanFactory () 方法访问ApplicationContextBeanFactory来完成的,该方法返回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 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.

Bean元数据和手动提供的单例实例(singleton instance)需要尽早注册,以便容器在自动装配和其他内省(introspection)步骤中正确地对它们进行推理。虽然在某种程度上支持覆盖现有元数据和现有单例实例,但官方不支持在运行时注册新的bean(与对工厂的实时访问同时进行),这可能导致并发访问异常、bean容器中的状态不一致,或两者兼而有之。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月满闲庭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值