AbstractApplicationContext#refresh
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
startupDate
closed(false)
、active(true)
PropertySource
AbstractPropertyResolver#requiredProperties
,默认为空,可设置earlyApplicationListeners
applicationListeners
earlyApplicationEvents
BeanFactory
,如果已存在的话DefaultListableBeanFactory
BeanFactory
属性 - customizeBeanFactory(beanFactory)
AbstractRefreshableApplicationContext#allowBeanDefinitionOverriding
AbstractRefreshableApplicationContext#allowCircularReferences
BeanFactory
到 Spring 应用上下文AbstractApplicationContext#prepareBeanFactory(ConfigurableListableBeanFactory) 方法
关联 ClassLoader
设置 Bean 表达式处理器
与 SpEL 表达式相关
org.springframework.context.expression.StandardBeanExpressionResolver
添加 PropertyEditorRegistrar 实现 - ResourceEditorRegistrar
org.springframework.beans.support.ResourceEditorRegistrar
添加 Aware 回调接口 BeanPostProcessor 实现 - ApplicationContextAwareProcessor
EnvironmentAware
EmbeddedValueResolverAware
ResourceLoaderAware
ApplicationEventPublisherAware
MessageSourceAware
ApplicationContextAware
忽略 Aware
回调接口作为依赖注入接口
注册 ResolvableDependency 对象 - BeanFactory 、ResourceLoader 、 ApplicationEventPublisher 以及 Applicationcontext
BeanFactory
是 ApplicationContext
关联的 BeanFactory
ResourceLoader
、ApplicationEventPublisher
以及 Applicationcontext
都是 ApplicationContext
添加 BeanPostProcessor - ApplicationListenerDetector
在 BeanPostProcessor#postProcessAfterInitialization
阶段,将单例的 ApplicationListener
加入 ApplicationContext
如果包含 beanName 是 loadTimeWeaver 的 bean,注册 BeanPostProcessor - LoadTimeWeaverAwareProcessor 对象,并设置容器的临时 ClassLoader
,AbstractBeanFactory#tempClassLoader
与 AOP 相关
注册单例对象 - Environment、Java System Properties 以及 OS 环境变量
environment
- ApplicationContext#environment
systemProperties
- (Map) System.getProperties()
systemEnvironment
- (Map) System.getenv()
AbstractApplicationContext#postProcessBeanFactory(ConfigurableListableBeanFactory) 方法
org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory) 方法
判断 BeanFactory 是不是 BeanDefinitionRegistry 的实例
DefaultListableBeanFactory
实现 BeanDefinitionRegistry
如果是,调用 BeanFactoryPostProcessor 或 BeanDefinitionRegistryPostProcessor 后置处理方法
BeanDefinitionRegistryPostProcessor
继承 BeanFactoryPostProcessor
BeanFactoryPostProcessor#postProcessBeanFactory
BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry
如果不是,只调用 BeanFactoryPostProcessor
后置处理方法 BeanFactoryPostProcessor#postProcessBeanFactory
如果包含 beanName 是 loadTimeWeaver 的 bean,注册 BeanPostProcessor - LoadTimeWeaverAwareProcessor 对象,并设置容器的临时 ClassLoader
,AbstractBeanFactory#tempClassLoader
执行顺序:
AbstractApplicationContext#beanFactoryPostProcessors
中的 BeanDefinitionRegistryPostProcessor
BeanFactory
中实现了 PriorityOrdered
的 BeanDefinitionRegistryPostProcessor
BeanBeanFactory
中实现了 Ordered
的 BeanDefinitionRegistryPostProcessor
BeanBeanFactory
中的 BeanDefinitionRegistryPostProcessor
BeanAbstractApplicationContext#beanFactoryPostProcessors
中的普通 BeanFactoryPostProcessor
BeanFactory
中 BeanDefinitionRegistryPostProcessor
PriorityOrdered
接口的 BeanFactoryPostProcessor
Ordered
接口的 BeanFactoryPostProcessor
BeanFactoryPostProcessor
PriorityOrdered
类型的 BeanPostProcessor
Beans -- 该bean会提前初始化Ordered
类型的 BeanPostProcessor
BeansBeanPostProcessor
BeansMergedBeanDefinitionPostProcessor
继承 BeanPostProcessor
,生命周期在 MergedBeanDefinition 后ApplicationListenerDetector
的顺序放到最后BeanFactory
中存在 beanName 为 messageSource
的 MessageSource
,则使用,否则注册 DelegatingMessageSource
MessageSource
内建依赖BeanFactory
中存在 beanName 为 applicationEventMulticaster
的 ApplicationEventMulticaster
,则使用,否则注册 SimpleApplicationEventMulticaster
ApplicationEventPublisher
底层实现org.springframework.web.context.support.AbstractRefreshableWebApplicationContext#onRefresh
org.springframework.web.context.support.GenericWebApplicationContext#onRefresh
org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext#onRefresh
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#onRefresh
org.springframework.web.context.support.StaticWebApplicationContext#onRefresh
ApplicationListener
对象(集合)BeanFactory
所注册 ApplicationListener
BeansAbstractApplicationContext#earlyApplicationEvents
AbstractApplicationContext#finishBeanFactoryInitialization(ConfigurableListableBeanFactory) 方法
BeanFactory
关联 ConversionService
Bean,如果存在
beanName 为 conversionService
的 ConversionService
添加 StringValueResolver
对象
如果 AbstractBeanFactory#embeddedValueResolvers
为空,添加一个
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
依赖查找 LoadTimeWeaverAware
Bean -- AOP
BeanFactory
临时 ClassLoader
置为 null
BeanFactory
冻结配置
DefaultListableBeanFactory#configurationFrozen
DefaultListableBeanFactory#frozenBeanDefinitionNames
BeanFactory
初始化非延迟单例 Beans
SmartInitializingSingleton
的生命周期, SmartInitializingSingleton#afterSingletonsInstantiated
ResourceLoader
缓存 - clearResourceCaches()
@since 5.0lifecycleProcessor
的 LifecycleProcessor
,则使用 DefaultLifecycleProcessor
LifecycleProcessor#onRefresh()
方法ContextRefreshedEvent
MBeanServer
托管 Live BeansLifecycle
BeansLifecycle
BeansContextStartedEvent
LifecycleProcessor
Lifecycle
BeansLifecycle
BeansContextStoppedEvent
AbstractApplicationContext#close()
方法active(false)
、closed(true)
LiveBeansView#unregisterApplicationContext(ConfigurableApplicationContext)
ContextCLosedEvent
Lifecycle
BeansLifecycle
BeansBeanFactory
onClose()
ConfigurableApplicationContext#refresh()
ConfigurableApplicationContext#start()
ConfigurableApplicationContext#stop()
ConfigurableApplicationContext#close()
ObjectFactory
(或 ObjectProvider
)可关联某一类型 BeanObjectFactory
和 ObjectProvider
对象在被依赖注入和依赖查询时并未实时查找关联类型的 BeanObjectFactory
(或 ObjectProvider
)调用 getObject()
方法时,目标 Bean 才被依赖查找ObjectFactory
(或 ObjectProvider
)相当于某一类型 Bean 依赖查找代理对象org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#singletonObjects
属性BeanDefinition
每次创建@Bean
方法@Bean
方法和实例 @Bean
方法AbstractAutowireCapableBeanFactory#setAllowCircularReferences
DefaultSingletonBeanRegistry#singletonFactories
AbstractAutowireCapableBeanFactory#getEarlyBeanReference
DefaultSingletonBeanRegistry#earlySingletonObjects