Spring 5.0.8.RELEASE文档 Core1-1.3 Bean 的概述

版权声明:mj1001001 https://blog.csdn.net/qq_42786889/article/details/81675963

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。这些bean是由你提供给容器的以及配置参数一起创建的,例如xml配置的

Within the container itself, these bean definitions are represented as BeanDefinition objects, which contain (among other information) the following metadata:
A package-qualified class name: typically the actual implementation class of the bean being defined.

1、Bean behavioral configuration elements, which state how the bean should behave in the container (scope, lifecycle callbacks, and so forth).
2、References to other beans that are needed for the bean to do its work; these references are also called collaborators or dependencies.
3、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.

This metadata translates to a set of properties that make up each bean definition.

在容器内部,这些bean定义是代表BeanDefinition类,它包含以下元数据:
一个合格的全路径名称:通常是指实现类。

1、bean的行为元素,用于描述bean在容器中的行为状态(scope, lifecycle callbacks等)(即bean的作为范围,生命周期中的创建销毁等回调)
2、关联依赖其他需要用到的bean,这些称之为依赖或者合作类
3、其他在bean实例化创建时设置的配置参数值,例如多个连接被线程池所管理需要设置线程池的大小参数。

这些元数据转换为一组属性来构成(组成)bean定义。

bean定义的属性表格
上面表格所在的连接

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,ApplicationContext的实现类也提供注册对象进入容器的方法。通过调用ApplicationContext的BeanFactory 中的getBeanFactory()方法,会返回DefaultListableBeanFactory 对象,这个对象提供registerSingleton()和 registerBeanDefinition()方法去注册容器外的对象进入容器。但是,通常应用都是通过读取配置文件而单独工作。(就是不推荐外部注册-,-)

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可能为null而无法访问或者并发时引用不一致,应该是这个意思吧,总之要尽早注册-,-)

猜你喜欢

转载自blog.csdn.net/qq_42786889/article/details/81675963