Spring拓展点ConfigurationClassPostProcessor解析流程

前言

ConfigurationClassPostProcessor是Spring提供的一个重要的拓展点,从类图上看:
1.实现BeanDefinitionRegistryPostProcessor,拥有了beanFactory后置处理器功能,可以注册BeanDefinition,也可以修改BeanDefinition信息
2.实现BeanClassLoaderAware可以获取类加载器
3.实现EnvironmentAware可以获取环境变量
4.ApplicationStartupAware可以设置应用启动过程各个步骤,收集关于它们的性能和上下文信息;
5.获取ResourceLoaderAware可以获取资源管理器
6.实现了PriorityOrdered接口,拥有优先级特性

ConfigurationClassPostProcessor

本文主要讲解它通过实现BeanDefinitionRegistryPostProcessor是如何解析bean类,向容器中注册BeanDefinition

​​​​在这里插入图片描述
从以下代码为入口,详解ConfigurationClassPostProcessor流程

public static void main(String[] args) {
		//1.实例Spring容器-AnnotationConfigApplicationContext
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		//2.注册AppConfig配置类
		context.register(AppConfig.class);
		//3.执行bean扫描,实例化非懒加载的单例bean
		context.refresh();
	}

AppConfig作为配置类,该类上添加了ComponentScan

package com.cyl;

import org.springframework.context.annotation.*;

@ComponentScan("com.cyl")
public class AppConfig {
}

1.引入时机

在new AnnotationConfigApplicationContext()时,会去实例化AnnotatedBeanDefinitionReader,这一阶段会去引入某些bean注册到容器中
在这里插入图片描述
执行注册功能时,会先注册ConfigurationClassPostProcessor类型的BeanDefinition到容器中
在这里插入图片描述

2.实例化时机

执行容器的refresh方法时,会执行到invokeBeanFactoryPostProcessors方法,在这个方法中会获取所有beanFactory后置处理器
在这里插入图片描述
执行invokeBeanFactoryPostProcessors方法会先根据类型BeanDefinitionRegistryPostProcessor找到符合的bean名称,再通过bean名称getBean,此时就会获取到ConfigurationClassPostProcessor实例化后的对象

在这里插入图片描述

3.作用时机

postProcessBeanDefinitionRegistry解析配置类,注册BeanDefinition到beanFactory中

实例化后调用invokeBeanDefinitionRegistryPostProcessors此时真正开始执行解析bean流程
在这里插入图片描述
执行到org.springframework.context.annotation.ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry开始解析类,主要流程分为6步
1.获取配置类,凡是类上添加了@Configuration,@Component,@ComponentScan,@Import,ImportResource,添加@Bean的方法。由于本示例中AppConfig类中添加了ComponentScan注解,所以解析出来的配置类为AppConfig
2.将获取到的配置类AppConfig封装为ConfigurationClass
3.解析配置类,添加configurationClasses属性中
4.进一步解析配置类中的属性,生成BeanDefinition注册到beanFactory
5.判断当前beanFactory的beanDefinition数量是否大于已解析的配置类数量,若大于则说明解析出了新的类,则需要递归从获取配置类再开始。若不大于则说明解析的类均已注册到了beanFactory。
具体的流程可以参考下图,花了2小时画的,有疑问可以提出来。
在这里插入图片描述

postProcessBeanFactory增强full配置类,代理添加了Configuration注解的配置类

待执行完invokeBeanDefinitionRegistryPostProcessors解析完配置类后,会执行invokeBeanFactoryPostProcessors会调用到postProcessBeanFactory方法,对full配置类添加代理。

由上面可知配置类是类上添加@Configuration,@Component,@ComponentScan,@Import,ImportResource注解,或者方法上添加@Bean的注解。
而@Configuration属性proxyBeanMethods设置为true,那配置类就是full配置类,其他的配置类为lite配置类。
postProcessBeanFactory方法主要是处理full配置类,为这些full配置类新增代理类
目前我还没有了解到这个full配置类的作用,后续学习到会继续补充。

总结

ConfigurationClassPostProcessor是一个bean工厂的后置处理器,它重写了两个方法,最重要的方法是postProcessBeanDefinitionRegistry,主要是解析配置类,生成BeanDefinition注册到bean工厂中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值