一、重要对象
流程涉及的几个重要对象
- StringValueResolver:bean工厂依赖此对象来解析配置值
- ConfigFileApplicationListener:读取配置文件并放入Context中
- PropertySourcesPlaceholderConfigurer:实现BeanFactoryPostProcessor接口,从context中获取配置,构建StringValueResolver并放入bean工厂
- PropertyPlaceholderAutoConfiguration:被标注了@Configuration注解,定义了PropertySourcesPlaceholderConfigurer bean
- AutowiredAnnotationBeanPostProcessor:拦截bean的创建处理@Value注解
二、流程概述
可以概括分为以下几步
- 读取配置文件放入Context
- 将PropertySourcesPlaceholderConfigurer对象放入容器
- 调用PropertySourcesPlaceholderConfigurer.postProcessBeanFactory()方法创建StringValueResolver对象放入bean工厂
- 将AutowiredAnnotationBeanPostProcessor放入容器
- 拦截bean工厂创建bean过程,通过StringValueResolver解析@Value注解,注入值
三、具体调用流程
- 读取配置文件放入Context
通过ConfigFileApplicationListener对象完成
- 将PropertySourcesPlaceholderConfigurer对象放入容器
- PropertyPlaceholderAutoConfiguration对象中定义了PropertySourcesPlaceholderConfigurer这个bean,而PropertyPlaceholderAutoConfiguration定义在spring-boot-autoconfigure-*.jar/META-INF/spring.factories文件中,在spring-boot扫描时被加载并解析
- 调用PropertySourcesPlaceholderConfigurer.postProcessBeanFactory()方法将配置文件数据放入bean工厂
- 将AutowiredAnnotationBeanPostProcessor放入容器
- 拦截bean工厂创建bean过程,通过StringValueResolver解析@Value注解,注入值