springBoot与Spring源码分析
2024-08-26 14:17:09 8 举报
springBoot源、Spring源码分析(未完成,后续补充)
作者其他创作
大纲/内容
springBoot扩展点ApplicationRunnerCommandLineRunner
预留方法,用于扩展
ConfigurableEnvironment environment = getOrCreateEnvironment()
AnnotationConfigApplicationContext(Class)
ConfigurationPropertySources.attach(environment)
reader 1.定义BeanDefinitionRegistry DefaultListableBeanFactory 2.BeanFactory后置处理器 xxxBeanFactoryPostProcessorscanner 1.默认扫描注解 @Component、继承@Component、@Named、@ManagedBean监听器处理器,处理@EventListnerEventListenerMethodProcessorDefaultEventListenerFactory
applyInitializers(context)
// Add boot specific singleton beansConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
<build><plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring‐boot‐maven‐plugin</artifactId> </plugin> </plugins> </build>生成fatjar
this()
bindToSpringApplication(environment)
1.获取Log日志实现2.获取spring.profiles.active
设置环境信息StandardServletEnvironment
listeners.started(context);
1.this.webApplicationType = WebApplicationType.deduceFromClasspath();2.setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));3.setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
获取beanFactory 设置属性beanFactory.registerSingleton(\"springApplicationArguments\
SpringBoot启动原理分析
postProcessApplicationContext(context)
添加默认扫描注解:@ManagedBean JSR-250@Name JSR-330@Component
ConfigurationClassPostProcessorAutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor监听器处理器,处理@EventListnerEventListenerMethodProcessorDefaultEventListenerFactoryDefaultListableBeanFactory 设置属性 AnnotationAwareOrderComparator ContextAnnotationAutowireCandidateResolver
this.scanner=new ClassPathBeanDefinitionScanner(this)
refreshContext(context)
没看明白有啥JB用
manifest文件Main-class:start-class:
initApplicationEventMulticaster()
springApplication设置环境信息
发布事件
1.设置属性 @Primary @Lazy @
invokeBeanFactoryPostProcessors(beanFactory)
run(args)
context设置环境信息
注册ConfigurableListableBeanFactory
获取ApplicationContextInitializer 执行Initializer方法spring-boot-autoconfiguer 添加BeanFactoryPostProcessor -> SharedMetadataReaderFactoryContextInitializer 添加监听器 -> ConditionEvaluationReportLoggingListenerspring-boot 添加BeanFactoryPostProcessor -> ConfigurationWarningsApplicationContextInitializer 注册ContextId -> ContextIdApplicationContextInitializer 初始化配置项 context.initializer.classes -> DelegatingApplicationContextInitializer ServerPortInfoApplicationContextInitializer -> 设置 local.server.port属性
getRunListeners(args)
发布spring.factories部分类
this.reader=new AnnotatedBeanDefinitionReader(this)
扩展BeanFactoryPostProcessorsgetBeanFactoryPostProcessors(),需要手动调用:AbstractApplicationContext.addBeanFactoryPostProcessor
加载spring.factories文件1.加载ApplicationListener2.加载配置文件*.properties
listeners.environmentPrepared(environment)
封装参数applicationArguments用于解析启动命令的参数
设置的默认配置文件到environment
注册多播器如果实现:ApplicationEventMulticaster否则默认:SimpleApplicationEventMulticasterSpring事件体系:事件、监听器、广播
finishBeanFactoryInitialization
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args)
register(componentClasses)
根据webApplicationType实例化:ConfigurableApplicationContextAnnotationConfigServletWebServerApplicationContextthis.reader = new AnnotatedBeanDefinitionReader(this);this.scanner = new ClassPathBeanDefinitionScanner(this);
用于加载jar包里面的jar
JarLauncher 类加载器
this.reader.register(componentClasses);
webApplicationType
context.setEnvironment(environment)
SpringBoot 通过Jar启动
if (this.logStartupInfo) { logStartupInfo(context.getParent() == null); logStartupProfileInfo(context);}
执行refresh方法
AnnotationConfigUtils#registerAnnotationConfigProcessors
1.推断当前容器类型,默认servlet2.设置初始化类型3.设置监听器
监听器设置环境信息
context 设置属性ResourceLoader、ClassLoader、ConversionService
mvn插件
registerDefaultFilters
设置spring.beaninfo.ignore属性(这JB属性不知道有啥用)
createApplicationContext()
Spring源码解析
refresh()
new SpringApplication(primarySources).run(args)
完成Bean注册
ConditionEvaluator
configureIgnoreBeanInfo(environment);
finishRefresh()
调用run方法
listeners.starting()
new SpringApplication(primarySources)
检测是MutablePropertySources 有 configurationProperties属性 移除 添加 configurationProperties 属性 即为配置文件
收藏
0 条评论
回复 删除
下一页