项目中用到
Object object = applicationContext.getBean(name)
在Spring中,所有的Bean都是由BeanFactory(也就是IOC容器)来进行管理的。
BeanFacotry
The root interface for accessing a Spring bean container.This is the basic client view of a bean container.
源码注释:用于访问Spring bean容器的根接口。这是bean容器的基本客户端视图
BeanFactory无法支持spring的许多插件,如AOP功能、Web应用等。
BeanFactory接口方法如下图
ApplicationContext
ApplicationContext是BeanFactory子接口
ApplicationContext以一种更向面向框架的方式工作以及对上下文进行分层和实现继承
Central interface to provide configuration for an application.
源码注释:用于为应用程序提供配置的中央接口。
BeanFacotry和ApplicationContext继承关系
BeanDefinition
A BeanDefinition describes a bean instance, which has property values,
constructor argument values, and further information supplied by
concrete implementations.
BeanDefinition描述了一个bean实例,它具有属性值,
构造函数参数值,要是提供的更多信息需
具体实现。
BeanDefinitionRegistry
Interface for registries that hold bean definitions, for example RootBeanDefinition
and ChildBeanDefinition instances. Typically implemented by BeanFactories that
internally work with the AbstractBeanDefinition hierarchy.
<p>This is the only interface in Spring's bean factory packages that encapsulates
<i>registration</i> of bean definitions. The standard BeanFactory interfaces
only cover access to a <i>fully configured factory instance</i>.
<p>Spring's bean definition readers expect to work on an implementation of this
interface. Known implementors within the Spring core are DefaultListableBeanFactory
and GenericApplicationContext.
包含bean定义的注册表的接口,例如RootBeanDefinition
和ChildBeanDefinition实例。 通常由BeanFactories实现
内部使用AbstractBeanDefinition层次结构。
<p>这是Spring封装的bean工厂包中唯一的接口
<i>注册</ i> bean定义。 标准的BeanFactory接口
仅涵盖对<i>完全配置的工厂实例</ i>的访问权限。
<p> Spring的bean定义读者希望能够实现这一点
界面。 Spring core中的已知实现者是DefaultListableBeanFactory
和GenericApplicationContext。
DefaultListableBeanFactory
interfaces: a full-fledged bean factory
based on bean definition objects.
<p>Typical usage is registering all bean definitions first (possibly read
from a bean definition file), before accessing beans. Bean definition lookup
is therefore an inexpensive operation in a local bean definition table,
operating on pre-built bean definition metadata objects.
<p>Can be used as a standalone bean factory, or as a superclass for custom
bean factories. Note that readers for specific bean definition formats are
typically implemented separately rather than as bean factory subclasses:
see for example {@link PropertiesBeanDefinitionReader} and
{@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}.
接口:一个成熟的bean工厂
基于bean定义对象。
<p>典型用法是首先注册所有bean定义(可能已读取
访问bean之前,来自bean定义文件)。 Bean定义查找
因此在本地bean定义表中是一个轻量的操作,
在预构建的bean定义元数据对象上运行。
<p>可以用作独立的bean工厂,也可以用作自定义的超类
bean工厂。 请注意,特定bean定义格式的readers是
通常单独实现,而不是作为bean工厂子类实现:
参见例如{@link PropertiesBeanDefinitionReader}和
{@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}。
总结
配置文件解析为BeanDefinition对象,被注册到了 DefaultListableBeanFactory(实现BeanDefinitionRegistry接口), 保存在beanDefinitionMap(ConcurrentHashMap)。
GenericApplicationContext 是ApplicationContext实现类,有final的DefaultListableBeanFactory成员变量,最终取到beanDefinitionMap中的bean。
spring全文:https://www.jianshu.com/p/619fe7164afb?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation
spring容器文:https://www.cnblogs.com/digdeep/p/4518571.html
spring源码深度解析—容器的基本实现:https://blog.csdn.net/uftjtt/article/details/79896110