【spring微服务】spring-boot启动过程之资源加载
spring-boot启动原理
https://blog.csdn.net/Mr_OOO/article/details/89477948
一、关键类
1、org.springframework.boot.BeanDefinitionLoader
class BeanDefinitionLoader { //服务配置的资源路径 private final Object[] sources; //注解加载器 private final AnnotatedBeanDefinitionReader annotatedReader; //xml配置加载器 private final XmlBeanDefinitionReader xmlReader; //groovy脚本加载器 private BeanDefinitionReader groovyReader; //类路径扫描器 private final ClassPathBeanDefinitionScanner scanner; //资源加载器 private ResourceLoader resourceLoader; //registry:其实就是org.springframework.beans.factory.support.DefaultListableBeanFactory容器 //服务配置的资源路径 BeanDefinitionLoader(BeanDefinitionRegistry registry, Object... sources) { Assert.notNull(registry, "Registry must not be null"); Assert.notEmpty(sources, "Sources must not be empty"); this.sources = sources; this.annotatedReader = new AnnotatedBeanDefinitionReader(registry); this.xmlReader = new XmlBeanDefinitionReader(registry); if (isGroovyPresent()) { this.groovyReader = new GroovyBeanDefinitionReader(registry); } this.scanner = new ClassPathBeanDefinitionScanner(registry); this.scanner.addExcludeFilter(new ClassExcludeFilter(sources)); } }
在初始化该类时,其中的类型为:AnnotatedBeanDefinitionReader的annotatedReader属性(org.springframework.context.annotation.AnnotatedBeanDefinitionReader)属性初始化的时候,会通过AnnotationConfigUtils向DefaultListableBeanFactory注入ConfigurationClassPostProcessor,CommonAnnotationBeanPostProcessor,AutowiredAnnotationBeanPostProcessor
【1】springboot的启动类,将来会作为一个资源,形成一个AnnotatedGenericBeanDefinition注册到spring容器中。
=========第一步:SpringApplication类=============== private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) { context.setEnvironment(environment); postProcessApplicationContext(context); applyInitializers(context); listeners.contextPrepared(context); if (this.logStartupInfo) { logStartupInfo(context.getParent() == null); logStartupProfileInfo(context); } // Add boot specific singleton beans ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); beanFactory.registerSingleton("springApplicationArguments", applicationArguments); if (printedBanner != null) { beanFactory.registerSingleton("springBootBanner", printedBanner); } if (beanFactory instanceof DefaultListableBeanFactory) { ((DefaultListableBeanFactory) beanFactory) .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } if (this.lazyInitialization) { context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor()); } // Load the sources Set<Object> sources = getAllSources(); Assert.notEmpty(sources, "Sources must not be empty"); //会将启动加载类,注册到BeanDefinitionLoader类中的annotatedReader属性中 load(context, sources.toArray(new Object[0])); listeners.contextLoaded(context); }
【2】AbstractApplicationContext的refresh()方法中的invokeBeanFactoryPostProcessors(beanFactory)会向spring容器中提前注入bean.
其中类:PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors()); 中会调用ConfigurationClassPostProcessor,会解析启动类上注解,依次向spring中注入应有的bean

- SharedMetadataReaderFactoryContextInitializer.CachingMetadataReaderFactoryPostProcessor
- ConfigurationWarningsApplicationContextInitializer.ConfigurationWarningsPostProcessor
- ConfigFileApplicationListener.PropertySourceOrderingPostProcessor
1)自动装配还是利用了SpringFactoriesLoader来加载META-INF/spring.factoires文件里所有配置的EnableAutoConfgruation,它会经过exclude和filter等操作,最终确定要装配的类
2) 处理@Configuration的核心还是ConfigurationClassPostProcessor,这个类实现了BeanFactoryPostProcessor, 因此当AbstractApplicationContext执行refresh方法里的invokeBeanFactoryPostProcessors(beanFactory)方法时会执行自动装配.详情见:https://www.cnblogs.com/niechen/p/9027804.html
2、org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition
3、org.springframework.core.type.StandardAnnotationMetadata
4、org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor(接口)
- org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer.CachingMetadataReaderFactoryPostProcessor
- org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer.ConfigurationWarningsPostProcessor
- org.springframework.boot.context.config.ConfigFileApplicationListener.PropertySourceOrderingPostProcessor
5、org.springframework.context.annotation.ConfigurationClassPostProcessor
->解析注解配置
6、org.springframework.context.annotation.ConfigurationClassUtils
->注解类的工具类
7、org.springframework.context.annotation.AnnotationConfigUtils
->注解配置工具类
二、加载过程
1、spring-boot服务启动时,会将启动类,组装成一个
org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition,用org.springframework.beans.factory.config.BeanDefinitionHolder包装并注入到org.springframework.beans.factory.support.DefaultListableBeanFactory中。
2、在刷线
三、重点注解
1、@EnableAutoConfiguration 的组合注解@Import
->该注解可以配置自动加载配置的类:实现的接口
->org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.
该类会加载META-INF/spring-autoconfigure-metadata.properties的的配置内容
还会加载META-INF/spring.factories 文件中key为:org.springframework.boot.autoconfigure.EnableAutoConfiguration.EnableAutoConfiguration的类 比如:org.springframework.boot.autoconfigure.aop.AopAutoConfiguration
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration
->会引入@EnableAspectJAutoProxy,其会引入@Import(AspectJAutoProxyRegistrar.class)
->从而引入:org.springframework.context.annotation.AspectJAutoProxyRegistrar
->向IOC中注入:org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator,对类进行动态代理。
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
->会向spring容器中自动注入:org.springframework.jdbc.datasource.DataSourceTransactionManager
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
->该类中存在@EnableTransactionManagement,该注解导入了@Import(TransactionManagementConfigurationSelector.class)
->org.springframework.transaction.annotation.TransactionManagementConfigurationSelector
->TransactionManagementConfigurationSelector返回了org.springframework.context.annotation.AutoProxyRegistrar,org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration
->ProxyTransactionManagementConfiguration导入了BeanFactoryTransactionAttributeSourceAdvisor(org.springframework.transaction.interceptor.TransactionInterceptor)
四、关键接口
org.springframework.boot.SpringApplication(springboot的项目主类)
org.springframework.context.ApplicationContextInitializer (初始化spring容器)
org.springframework.context.ApplicationListener(容器监听器)
=>org.springframework.boot.context.config.ConfigFileApplicationListener(配置文件监听器)
=>
org.springframework.boot.env.EnvironmentPostProcessor(环境处理器)
=>org.springframework.boot.context.config.ConfigFileApplicationListener(处理环境数据)
org.springframework.boot.env.PropertySourceLoader(环境配置文件加载器)
=>org.springframework.boot.env.PropertiesPropertySourceLoader
=>org.springframework.boot.env.YamlPropertySourceLoader
浙公网安备 33010602011771号