一、简洁模型

image.png

二、详细描述

image.png
图中虚线部分的new和反射,并没有那么简单,这个过程可以通过BeanfactoryPostProcessor进行功能的扩展,对bean的定义和值。

1、Beandefinition

描述bean的信息

2、BeanFactory

访问bean容器的一个接口(The root interface for accessing a Spring bean container.)
image.png
beanDefinitionMap 存放bean
参考 https://www.yuque.com/wangchao-volk4/fdw9ek/vf0eas#C9aYj
image.png

3、BeanfactoryPostProcessor

工厂钩子,允许自定义修改应用程序上下文的bean定义,调整上下文的底层bean工厂的bean属性值。
例如子类PlaceholderConfigurerSupport,可以替换占位符${jdbc.username}

Factory hook that allows for custom modification of an application context’s bean definitions, adapting the bean property values of the context’s underlying bean factory.

也可以自己实现BeanfactoryPostProcessor,对bean做一些处理。
image.png

三、思考

基于上诉描述,我们工作中用到的最多的不是配置文件初始化bean的形式,而是用注解的形式,那这些注解是怎么实现的?
以springboot为例
找到 方法 ConfigurationClassPostProcessor (它实现了BeanDefinitionRegistryPostProcessor
�继承了BeanFactoryPostProcessor),里面的方法 postProcessBeanDefinitionRegistry —>parse—>parse->doProcessConfigurationClass
看如下代码可以知道其对注解进行了解析
image.png