Spring源码分析——容器启动过程(开局一个类,进化全靠配)

目录

context初始化

refresh()


入口

SpringApplication.run(XXApplication.class, args);

加载spring.factories

SpringApplication#createApplicationContext  

prepareContext 会注册应用bean到beanDefinitionMap

org.springframework.context.support.AbstractApplicationContext#refresh

context初始化

applicationContext

beanFactory

refresh()

1. prepareRefresh 

// Prepare this context for refreshing

2. obtainFreshBeanFactory

// Tell the subclass to refresh the internal bean factory.

3. prepareBeanFactory

// Prepare the bean factory for use in this context. Configure the factory's standard context characteristics, such as the context's ClassLoader and post-processors

3.1.设置BeanClassLoader,BeanExpressionResolver,添加ResourceEditorRegistrar和ApplicationContextAwareProcessor

3.2.依赖检查和自动装配忽略的类型,*Aware类

3.3.某些类型和实例map,这些类型为BeanFactory,ResourceLoader,ApplicationEventPublisher,ApplicationContext(后三个即当前context对象实例)

3.4.添加BeanPostProcessor类,ApplicationListenerDetector

3.5.检测LoadTimeWeaver

3.6.设置默认的environment beans

4. postProcessBeanFactory

// Allows post-processing of the bean factory in context subclasses

5. invokeBeanFactoryPostProcessors

5.1 First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered

org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getAutoConfigurationEntry

这一步是很重要的一个方法,从入口类解析这个类上的注解,包含registrar和importSelector。解析AutoConfigurationImportSelector类时从配置文件加载所有的EnableAutoConfiguration类,同时从spring-autoconfigure-metadata.properties文件获取到配置类中的规则定义,并按照conditionalOn规则加载bean定义

        boolean[] match = filter.match(candidates, autoConfigurationMetadata);
		// 自动配置核心方法
		for (int i = 0; i < match.length; i++) {
			
			if (!match[i]) {
				
				skip[i] = true;
				
				candidates[i] = null;
				
				skipped = true; 
			}
		}
OnClassCondition

private ConditionOutcome getOutcome(String className, ClassLoader classLoader) {
			if (ClassNameFilter.MISSING.matches(className, classLoader)) {
				return ConditionOutcome.noMatch(ConditionMessage.forCondition(ConditionalOnClass.class)
						.didNotFind("required class").items(Style.QUOTE, className));
			}
			return null;
		}

参考:https://juejin.im/post/6844904080872046599

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值