springboot的run思路图
2024-04-29 14:39:06 0 举报
AI智能生成
spring启动的源码分析
作者其他创作
大纲/内容
SpringApplication.run
prepareContext
load->createBeanDefinitionLoader
AnnotatedBeanDefinitionReader
AnnotationConfigUtils.registerAnnotationConfigProcessor
初始化内置的BeanPostProcessor
初始化内置的BeanPostProcessor
ConfigurationClassPostProcessor
实现了BeanDefinitionRegistryPostProcessor,会最先加载
实现了BeanDefinitionRegistryPostProcessor,会最先加载
AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor
EventListenerMethodProcessor
DefaultEventListenerFactory
XmlBeanDefinitionReader
用于加载xml中定义的bean
ClassPathBeanDefinitionScanner
自定义扫描包下的类,手动注入bean
refreshContext
AbstractApplicationContext.refresh
prepareBeanFactory
工厂bean属性赋值,重点是添加了ApplicationContextAwareProcessor
postProcessBeanFactory
空方法,可继承用于扩展
invokeBeanFactoryPostProcessors
加载内置的BeanDefinitionRegistryPostProcessor
加载内置的BeanFactoryPostProcessor
PropertySourcesPlaceholderConfigurer
针对当前 Spring Environment及其PropertySources集解析 bean 定义属性值
和@Value注释中的 ${...} 占位符。
和@Value注释中的 ${...} 占位符。
1.加载自定义的BeanDefinitionRegistryPostProcessor
invokeBeanDefinitionRegistryPostProcessors调用postProcessBeanDefinitionRegistry
invokeBeanFactoryPostProcessors调用postProcessBeanFactory
2.加载自定义的BeanFactoryPostProcessor
invokeBeanFactoryPostProcessors调用postProcessBeanFactory
registerBeanPostProcessors
1.priorityOrderedPostProcessors(不包含MergedBeanDefinitionPostProcessor类型)
2.orderedPostProcessors(不包含MergedBeanDefinitionPostProcessor类型)
3.nonOrderedPostProcessors(不包含MergedBeanDefinitionPostProcessor类型)
4.internalPostProcessors(就是MergedBeanDefinitionPostProcessor类型)
initApplicationEventMulticaster
registerListeners
finishBeanFactoryInitialization
DefaultListableBeanFactory.preInstantiateSingletons
0.判断是否实现FactoryBean
FactoryBean
按照普通bean的方式创建bean
通过BeanFactory.getBean获取有区别
直接获取getObjectForBeanInstance
getBean("&"+beanName)的方式
判断是FactoryBean后获取之前bean实例化的原始Bean
判断是FactoryBean后获取之前bean实例化的原始Bean
getBean(beanName)会调用getObjectFromFactoryBean
然后调用实现类的getObject方法,并将返回的实例放到
factoryBeanObjectCache,方便下次调用
然后调用实现类的getObject方法,并将返回的实例放到
factoryBeanObjectCache,方便下次调用
普通的Bean
1.AbstractBeanFactory
getBean
doGetBean
getSingleton(beanName,createBean)
AbstractAutowireCapableBeanFactory.createBean
1.resolveBeforeInstantiation
判断hasInstantiationAwareBeanPostProcessors()
是否实现InstantiationAwareBeanPostProcessor
是否实现InstantiationAwareBeanPostProcessor
2.doCreateBean
createBeanInstance实例化
applyMergedBeanDefinitionPostProcessors
AutowiredAnnotationBeanPostProcessor.postProcessMergedBeanDefinition
CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition
此处加载的@PostConstruct,不调用
此处加载的@PostConstruct,不调用
populateBean属性赋值
判断hasInstantiationAwareBeanPostProcessors()
是否实现InstantiationAwareBeanPostProcessor
调用postProcessAfterInstantiation 如果返回false直接返回,不进行属性填充
是否实现InstantiationAwareBeanPostProcessor
调用postProcessAfterInstantiation 如果返回false直接返回,不进行属性填充
实现InstantiationAwareBeanPostProcessor
调用postProcessAfterInstantiation 如果返回true
调用postProcessProperties自定义属性填充
调用postProcessAfterInstantiation 如果返回true
调用postProcessProperties自定义属性填充
没实现InstantiationAwareBeanPostProcessor 正常填充属性
initializeBean初始化
invokeAwareMethods
BeanNameAware
调用setBeanName
调用setBeanName
BeanClassLoaderAware
调用setBeanClassLoader
调用setBeanClassLoader
BeanFactoryAware
调用setBeanFactory
调用setBeanFactory
applyBeanPostProcessorsBeforeInitialization
调用BeanPostProcessor的postProcessBeforeInitialization
调用BeanPostProcessor的postProcessBeforeInitialization
其中一个spring内置的BeanPostProcessor
ApplicationContextAwareProcessor
ApplicationContextAwareProcessor
EnvironmentAware
EmbeddedValueResolverAware
ResourceLoaderAware
ApplicationEventPublisherAware
MessageSourceAware
ApplicationStartupAware
ApplicationContextAware
invokeInitMethods
调用实现了InitializingBean的afterPropertiesSet
调用Bean的initMethod
applyBeanPostProcessorsAfterInitialization
调用BeanPostProcessor的postProcessAfterInitialization
调用BeanPostProcessor的postProcessAfterInitialization
2.DefaultListableBeanFactory
SmartInitializingSingleton
getBean之后循环所有的bean
调用实现了SmartInitializingSingleton的afterSingletonsInstantiated
getBean之后循环所有的bean
调用实现了SmartInitializingSingleton的afterSingletonsInstantiated
finishRefresh
publishEvent(new ContextRefreshedEvent(this))
发布事件,可以在此处监听,用于bean创建完成之后的扩展
afterRefresh
空方法,可继承用于扩展
listeners.started(context)
可以扩展SpringApplicationRunListener
不常用
不常用
callRunners
ApplicationRunner
CommandLineRunner
收藏
0 条评论
下一页