【Spring 学习笔记】7 Spring启动配置类源码解析

spring源码版本 spring-framework-5.3.10

源码思维导图

https://www.processon.com/v/64b0f88ab856374c5c180238

解析配置类的代码路径

AnnotationConfigApplicationContext(Class) -> refresh() -> invokeBeanFactoryPostProcessors() -> invokeBeanFactoryPostProcessors(beanFactory,beanFactoryPostProcessors)

ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry()代码块解析

再如下 代码块 1 中当执行到invokeBeanDefinitionRegistryPostProcessors()传入的参数ConfigurationClassPostProcessor这个Bean的时候,会在调用它的postProcessBeanDefinitionRegistry()方法中去解析配置类,方法实现 具体如下代码块2 所示

// 代码块 1
public static void invokeBeanFactoryPostProcessors(
			ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {   // BeanFactoryPostProcessor、BeanDefinitionRegistryPostProcessor
		
			// ......代码略
		
			// 执行扫描出来的BeanDefinitionRegistryPostProcessor
			// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
			String[] postProcessorNames =
					beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
			for (String ppName : postProcessorNames) {
				if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					processedBeans.add(ppName);
				}
			}
			// 升序排序
			sortPostProcessors(currentRegistryProcessors, beanFactory);
			registryProcessors.addAll(currentRegistryProcessors);
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
			currentRegistryProcessors.clear();

		// ......代码略
	}
// 代码块 2
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
		int registryId = System.identityHashCode(registry);
		if (this.registriesPostProcessed.contains(registryId)) {
			throw new IllegalStateException(
					"postProcessBeanDefinitionRegistry already called on this post-processor against " + registry);
		}
		if (this.factoriesPostProcessed.contains(registryId)) {
			throw new IllegalStateException(
					"postProcessBeanFactory already called on this post-processor against " + registry);
		}
		this.registriesPostProcessed.add(registryId);

		// 解析配置类
		processConfigBeanDefinitions(registry);
	}

解析配置类processConfigBeanDefinitions的方法代码分析

思路:

  • 判断哪些是配置类- checkConfigurationClassCandidate()
    • 查看是否有注解-@Configuration
      存在@Configuration,并且proxyBeanMethods不为false(为true或为null)时,就是Full配置类
      存在@Configuration,并且proxyBeanMethods为false时,是lite配置类
    • 只要存在@Component、@ComponentScan、@Import、@ImportResource四个中的一个,就是lite配置类
    • 只要存在@Bean注解了的方法,就是lite配置类
  • 解析配置类,将解析得到的BeanDefinition注册到容器中
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值