上篇回顧
上一篇prepareContext()準(zhǔn)備應(yīng)用上下文中分析了spring容器的準(zhǔn)備, 共計(jì)執(zhí)行了如下8步:
- 統(tǒng)一了ApplicationContext和Application的environment
- 設(shè)置ApplicationContext的beanNameGenerator,resouceLoader和classLoader, 并設(shè)置beanFactory的類型轉(zhuǎn)換Service
- 執(zhí)行Initializer
- 發(fā)布ApplicationContextInitializedEvent
- 打印啟動(dòng)日志和profile日志
- 手工注冊(cè)命令行和banner兩個(gè)單例Bean
- 初始化BeanDefinitionLoader, 加載啟動(dòng)類sources
- 發(fā)布contextLoaded事件
目錄
1. 刷新應(yīng)用上下文
2. AbstractApplicationContext#refresh
3. 準(zhǔn)備刷新 prepareRefresh()
????3.1 AnnotationConfigServletWebServerApplicationContext
????3.2 AbstractApplicationContext
4. 獲取并刷新beanFactory
5. 準(zhǔn)備beanFactory
6. beanFactory的后置處理器
7. 實(shí)現(xiàn)beanFactory的后置處理器
8.注冊(cè)bean處理器
9.國(guó)際化 initMessageSource()
10. 注冊(cè)事件發(fā)布器
11. 通知子類刷新刷新容器 onRefresh()
????ServletWebServerApplicationContext
????GenericWebApplicationContext
12.注冊(cè)監(jiān)聽器 registerListeners()
13. 完成beanFactory初始化, 實(shí)例化所有非延遲加載的bean
14.完成刷新 finishRefresh() 發(fā)布ContextRefreshedEvent
15. 異常處理
16. 清除緩存 resetCommonCaches()
1. 刷新應(yīng)用上下文
public class SpringApplication {
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
//本文的重點(diǎn)
//刷新應(yīng)用上下文
refreshContext(context);
//...
}
//刷新應(yīng)用上下文,注冊(cè)關(guān)閉應(yīng)用鉤子
private void refreshContext(ConfigurableApplicationContext context) {
refresh(context);
if (this.registerShutdownHook) {
//registerShutdownHook默認(rèn)為true
try {
//注冊(cè)shutdown方法
context.registerShutdownHook();
}
catch (AccessControlException ex) { }
}
}
//刷新應(yīng)用上下文
//調(diào)用AbstractApplicationContext#refresh方法
protected void refresh(ApplicationContext applicationContext) {
Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);
((AbstractApplicationContext) applicationContext).refresh();
}
}
2. AbstractApplicationContext#refresh
/**
* 抽象父類ApplicationContext
*/
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
//準(zhǔn)備刷新
prepareRefresh();
//刷新beanFactory
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
//準(zhǔn)備beanFactory
prepareBeanFactory(beanFactory);
try {
//beanFactory的后置處理器
postProcessBeanFactory(beanFactory);
//實(shí)現(xiàn)beanFactory的后置處理器
invokeBeanFactoryPostProcessors(beanFactory);
//beanFactory注冊(cè)后置處理器
registerBeanPostProcessors(beanFactory);
//初始化messageSource
initMessageSource();
//初始化Application事件發(fā)布器
initApplicationEventMulticaster();
//初始化其他特殊的bean
onRefresh();
//注冊(cè)監(jiān)聽器
registerListeners();
//完成beanFactory初始化
finishBeanFactoryInitialization(beanFactory);
//完成刷新,發(fā)布完成事件
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
//打印告警日志
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
//捕獲到異常,銷毀已經(jīng)注冊(cè)的bean
destroyBeans();
//捕獲到異常,取消刷新
cancelRefresh(ex);
//拋出異常
throw ex;
}
finally {
//重置緩存
resetCommonCaches();
}
}
}
}
3. 準(zhǔn)備刷新 prepareRefresh()
準(zhǔn)備刷新, 執(zhí)行了兩步:
- 調(diào)用當(dāng)前類AnnotationConfigServletWebServerApplicationContext的prepareRefresh()方法
- 顯式調(diào)用super.prepareRefresh()方法,也就是AbstractApplicationContext的prepareRefresh方法
3.1 AnnotationConfigServletWebServerApplicationContext#prepareRefresh
清空CachingMetadataReaderFactory中的緩存
public class AnnotationConfigServletWebServerApplicationContext
extends ServletWebServerApplicationContext implements AnnotationConfigRegistry {
//準(zhǔn)備刷新
@Override
protected void prepareRefresh() {
//如果類掃描器中的metadataReaderFactoryy是CachingMetadataReaderFactory的子類
//那么清除類掃描器中beanFactory維護(hù)的所有的bean
this.scanner.clearCache();
//顯式調(diào)用父類AbstractApplicationContext的prepareRefresh方法
super.prepareRefresh();
}
}
3.2 AbstractApplicationContext#prepareRefresh
設(shè)置刷新開始事件, 設(shè)置closed為false, active為true, 標(biāo)記容器處于active狀態(tài)
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {
//準(zhǔn)備刷新
protected void prepareRefresh() {
//記錄開始時(shí)間,調(diào)整active狀態(tài)
this.startupDate = System.currentTimeMillis();
this.closed.set(false);
this.active.set(true);
if (logger.isDebugEnabled()) {
//打印debug日志
if (logger.isTraceEnabled()) {
logger.trace("Refreshing " + this);
}
else {
logger.debug("Refreshing " + getDisplayName());
}
}
//初始化占位符,例如:$,#,{}
//GenericWebApplicationContext#initPropertySources
initPropertySources();
//當(dāng)前environment為StandardServletEnvironment
//調(diào)用AbstractEnvironment#validateRequiredProperties()方法
//然后調(diào)用AbstractPropertyResolver#validateRequiredProperties()方法
//如果屬性中缺少requiredProperties
//那么拋出MissingRequiredPropertiesException
getEnvironment().validateRequiredProperties();
if (this.earlyApplicationListeners == null) {
//如果earlyApplicationListeners為null
//保存applicationListeners
this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
}
else {
//如果earlyApplicationListeners不為null
//將earlyApplicationListeners同步到applicationListeners中
this.applicationListeners.clear();
this.applicationListeners.addAll(this.earlyApplicationListeners);
}
//初始化earlyApplicationEvents
this.earlyApplicationEvents = new LinkedHashSet<>();
}
}
4. 獲取并刷新beanFactory
AbstractApplicationContext中定義了模板方法, refreshBeanFactory和getBeanFactory調(diào)用的是GenericApplicationContext中實(shí)現(xiàn)的方法
public abstract class AbstractApplicationContext extends DefaultResourceLoader{
//id獲取
private String id = ObjectUtils.identityToString(this);
//刷新并返回beanFactory
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory();
return getBeanFactory();
}
@Override
public String getId() {
return this.id;
}
}
GenericApplicationContext#refreshBeanFactory
更新this.refreshed字段為true, 表示已經(jīng)更新了, 然后beanFactory設(shè)置serializationId, 最后返回beanFactory
public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry {
//內(nèi)部的beanFactory
private final DefaultListableBeanFactory beanFactory;
//原子bool值
private final AtomicBoolean refreshed = new AtomicBoolean();
//刷新beanFactory
@Override
protected final void refreshBeanFactory() throws IllegalStateException {
//原子自旋, 更新refreshed為true
if (!this.refreshed.compareAndSet(false, true)) {
throw new IllegalStateException(
"GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");
}
//調(diào)用AbstractApplicationContext的getId()方法
this.beanFactory.setSerializationId(getId());
}
//返回beanFactory
@Override
public final ConfigurableListableBeanFactory getBeanFactory() {
return this.beanFactory;
}
}
5. 準(zhǔn)備beanFactory
beanFactory是GenericApplicationContext中DefaultListableBeanFactory類型的成員變量, 設(shè)置beanFactory, 一共執(zhí)行了
- 配置classLoader為當(dāng)前context的classLoader
- 設(shè)置BeanExpressionResolver, 解析EL表達(dá)式
- 設(shè)置屬性編輯器
- 添加BeanPostProcessor
- 配置自動(dòng)裝配
- 手工注冊(cè)environment相關(guān)bean
public abstract class AbstractApplicationContext extends DefaultResourceLoader{
protected void prepareBeanFactory()(ConfigurableListableBeanFactory beanFactory) {
//beanFactory設(shè)置為當(dāng)前context的classLoader
beanFactory.setBeanClassLoader(getClassLoader());
//beanFactory設(shè)置BeanExpressionResolver,解析EL表達(dá)式
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
//beanFactory配置屬性編輯器,也就是類型轉(zhuǎn)換器
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
//ApplicationContextAwareProcessor會(huì)在加載Aware接口的時(shí)候, 傳遞參數(shù)到相應(yīng)的Aware實(shí)現(xiàn)類中
//所以需要忽略Aware接口的自動(dòng)裝配
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
//配置忽略自動(dòng)裝配
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
// 配置自動(dòng)裝配規(guī)則, 例如@Autowired注解注入
// 如果需要BeanFactory,那么注入beanFactory
// 如果需要ResourceLoader,ApplicationEventPublisher,ApplicationContext,則注入this
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
//添加ApplicationListenerDetector的beanPostProcessor
//ApplicationListenerDetector會(huì)檢測(cè)實(shí)現(xiàn)了ApplicationListener接口的bean
//在這些bean在實(shí)例化的過程中,記錄到singletonNames 中
//在bean被銷毀的時(shí)候, 從applicationContext的applicationEventMulticaster中移除
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
//添加LoadTimeWeaverAwareProcessor
//解析@EnableLoadTimeWeaving注解
//在類加載到JVM中時(shí), 織入切面
if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
//添加bean處理器,實(shí)現(xiàn)對(duì)AspectJ的支持
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
//設(shè)置classloader
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
// 手工注冊(cè)environment的相關(guān)bean
if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
}
if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
}
if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
}
}
}
6. beanFactory的后置處理器 postProcessBeanFactory(beanFactory)
后續(xù)處理各個(gè)beanFactory, 當(dāng)前applicationContext是AnnotationConfigServletWebServerApplicationContext的實(shí)例, postProcessBeanFactory執(zhí)行了三步
- 調(diào)用了父類方法
- 判斷basePackages是否為空, 不為空的話, 掃描this.basePackages
- 判斷annotatedClasses是否為空, 不為空的話, 注冊(cè)bean
AnnotationConfigServletWebServerApplicationContext#postProcessBeanFactory
public class AnnotationConfigServletWebServerApplicationContext
extends ServletWebServerApplicationContext implements AnnotationConfigRegistry {
//處理beanFactory
@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
//顯式調(diào)用父類ServletWebServerApplicationContext#postProcessBeanFactory
super.postProcessBeanFactory(beanFactory);
//basePackages默認(rèn)為null
if (this.basePackages != null && this.basePackages.length > 0) {
//不為空的話,進(jìn)行掃描
this.scanner.scan(this.basePackages);
}
//annotatedClasses默認(rèn)為空
if (!this.annotatedClasses.isEmpty()) {
//不為空的話注冊(cè)類
this.reader.register(ClassUtils.toClassArray(this.annotatedClasses));
}
}
}
ServletWebServerApplicationContext#postProcessBeanFactory
進(jìn)行了兩個(gè)操作, 首先添加了一個(gè)WebApplicationContextServletContextAwareProcessor的Aware Bean處理器, ServletContextAware的子類Bean在實(shí)例化過程中, 會(huì)被注入servletContext和servletConfig對(duì)象, 然后beanFactory中注冊(cè)了request和session兩個(gè)scopes, 注冊(cè)了幾個(gè)Autowired依賴類
public class ServletWebServerApplicationContext extends GenericWebApplicationContext
implements ConfigurableWebServerApplicationContext {
@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
//在ServletContext和ServletConfig初始化之前
//進(jìn)行bean處理
beanFactory.addBeanPostProcessor(
new WebApplicationContextServletContextAwareProcessor(this));
//忽略ServletContextAware自動(dòng)配置
beanFactory.ignoreDependencyInterface(ServletContextAware.class);
//beanFactory注冊(cè)web應(yīng)用scopes
//request和session
registerWebApplicationScopes();
}
}
WebApplicationContextUtils#registerWebApplicationScopes
注冊(cè)了request, session兩個(gè)scope, 然后注冊(cè)ServletRequest, ServletResponse, HttpSession, WebRequest
public abstract class WebApplicationContextUtils# {
public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory) {
//傳入ServletContext為null
registerWebApplicationScopes(beanFactory, null);
}
public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory,
@Nullable ServletContext sc) {
//注冊(cè)Scope
//request
beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
//session
beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope());
if (sc != null) {
//傳入sc為null
//注冊(cè)application scope
ServletContextScope appScope = new ServletContextScope(sc);
beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
// 設(shè)置ServletContext屬性
sc.setAttribute(ServletContextScope.class.getName(), appScope);
}
//注冊(cè)幾個(gè)Autowired自動(dòng)裝配
//ServletRequest.class
beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());
//ServletResponse.class
beanFactory.registerResolvableDependency(ServletResponse.class, new ResponseObjectFactory());
//HttpSession.class
beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());
//WebRequest.class
beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
if (jsfPresent) {
//jsfPresent默認(rèn)為false
FacesDependencyRegistrar.registerFacesDependencies(beanFactory);
}
}
}
7. 實(shí)現(xiàn)beanFactory的后置處理器
BeanFactoryPostProcessor是一個(gè)接口, 處理beanFactory中所有的bean, 在所有的beanDefinition加載完成之后, BeanFactoryPostProcessor可以對(duì)beanDefinition進(jìn)行屬性的修改, 之后再進(jìn)行bean實(shí)例化
BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口, 定義了postProcessBeanDefinitionRegistry方法, 會(huì)在postProcessBeanFactory方法執(zhí)行之前, 獲取bean定義, 并注冊(cè)到spring容器中
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
//處理器代理類
//處理this.beanFactoryPostProcessors中維護(hù)的bean處理器
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
//TempClassLoader為空
//包含了LoadTimeWeaver(加載到JVM時(shí), 進(jìn)行切面織入)
if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
}
7.1 PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors()
如果beanFactory是BeanDefinitionRegistry的子類, 按優(yōu)先級(jí)處理BeanDefinitionRegistryPostProcessor類型的后置處理器, 最后處理傳入的其他類型后置處理器, 處理流程如下:
首先處理傳入的beanFactoryPostProcessors, 將傳入的beanFactoryPostProcessors分為兩個(gè)部分
- BeanDefinitionRegistryPostProcessor子類, 執(zhí)行其postProcessBeanDefinitionRegistry方法, 然后維護(hù)到registryProcessors的列表中
- 非BeanDefinitionRegistryPostProcessor子類, 維護(hù)到regularPostProcessors列表中
然后處理beanFactory中注冊(cè)的BeanDefinitionRegistryPostProcessor類型的bean
- 優(yōu)先處理實(shí)現(xiàn)了PriorityOrdered接口的BeanDefinitionRegistryPostProcessor, 一共分為5個(gè)步驟
- 生成bean, 維護(hù)到currentRegistryProcessors列表中
- processedBeans中添加bean名稱, 標(biāo)記已經(jīng)被處理
- currentRegistryProcessors列表排序, 并添加到registryProcessors中
- 遍歷registryProcessors列表, 調(diào)用每個(gè)元素的postProcessBeanDefinitionRegistry方法
- 然后清空currentRegistryProcessors
- 然后處理實(shí)現(xiàn)了Ordered接口的BeanDefinitionRegistryPostProcessor, 一共分為5個(gè)步驟
- 先判斷processedBeans中是否包含了beanName, 如果包含了就不處理
- 生成bean, 維護(hù)到currentRegistryProcessors列表中
- processedBeans中添加bean名稱, 標(biāo)記已經(jīng)被處理
- currentRegistryProcessors列表排序, 并添加到registryProcessors中
- 遍歷registryProcessors列表, 調(diào)用每個(gè)元素的postProcessBeanDefinitionRegistry方法
- 然后清空currentRegistryProcessors
- 處理剩下的所有BeanDefinitionRegistryPostProcessor, 方法同第2步
- 遍歷registryProcessors, 調(diào)用其postProcessBeanFactory方法
- 遍歷所有的regularPostProcessors, 調(diào)用其postProcessBeanFactory方法
如果beanFactory不是BeanDefinitionRegistry的子類, 那么直接遍歷傳入的傳入的beanFactoryPostProcessors, 調(diào)用元素的postProcessBeanFactory方法
最后處理beanFactory中注冊(cè)的其他類型的BeanFactoryPostProcessor, 獲取bean名稱, 維護(hù)到postProcessorNames列表中, 之后的處理步驟如下:
- 首先處理實(shí)現(xiàn)了PriorityOrdered接口的BeanFactoryPostProcessor
- 獲取bean, 維護(hù)到priorityOrderedPostProcessors列表中
- priorityOrderedPostProcessors排序
- 遍歷priorityOrderedPostProcessors, 執(zhí)行其postProcessBeanFactory方法
- 然后處理實(shí)現(xiàn)了Ordered接口的BeanFactoryPostProcessor, 方法同上
- 最后處理其他BeanFactoryPostProcessor, 方法同上
ConfigurationClassPostProcessor處理了@Configuration注解, 掃描項(xiàng)目中的BeanDefinition, 這篇文章 詳細(xì)剖析了ConfigurationClassPostProcessor的源碼
final class PostProcessorRegistrationDelegate {
/**
* @param beanFactory
* @param beanFactoryPostProcessors有三個(gè):
* CachingMetadataReaderFactoryPostProcessor
* ConfigurationWarningsPostProcessor
* PropertySourceOrderingPostProcessor
*/
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
//processedBeans記錄處理過的bean名稱
Set<String> processedBeans = new HashSet<>();
if (beanFactory instanceof BeanDefinitionRegistry) {
//優(yōu)先處理傳入的BeanDefinitionRegistryPostProcessor
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
//非BeanDefinitionRegistryPostProcessor類型, 常規(guī)BeanFactory后置處理器
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
//BeanDefinitionRegistryPostProcessor類型的bean定義注冊(cè)器后置處理器
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
//regularPostProcessors有1個(gè):PropertySourceOrderingPostProcessor
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
//bean定義注冊(cè)器后置處理器,有如下兩個(gè):
//CachingMetadataReaderFactoryPostProcessor
//ConfigurationWarningsPostProcessor
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
//執(zhí)行其postProcessBeanDefinitionRegistry方法
registryProcessor.postProcessBeanDefinitionRegistry(registry);
//然后加入到registryProcessors中
registryProcessors.add(registryProcessor);
}
else {
//如果不是BeanDefinitionRegistryPostProcessor
//那么放入regularPostProcessors(常規(guī)后置處理器)中
regularPostProcessors.add(postProcessor);
}
}
//處理beanFactory中注冊(cè)的BeanDefinitionRegistryPostProcessor
//當(dāng)前正在處理的PostProcessor, 處理完成之后會(huì)清空
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
//將其名稱放入postProcessorNames數(shù)組中
//當(dāng)前只能獲取到一個(gè)bean:
//名稱為org.springframework.context.annotation.internalConfigurationAnnotationProcessor
//類型為ConfigurationClassPostProcessor
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
//優(yōu)先處理PriorityOrdered接口的BeanDefinitionRegistryPostProcessor
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
//處理完成之后, 放到processedBeans列表
processedBeans列表中.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
//添加到registryProcessors
registryProcessors.addAll(currentRegistryProcessors);
//遍歷currentRegistryProcessors,調(diào)用其postProcessBeanDefinitionRegistry方法
//執(zhí)行ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry, 掃描并注冊(cè)模塊中@Configuration注解的bean
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
//清空currentRegistryProcessors
currentRegistryProcessors.clear();
// 實(shí)現(xiàn)Order注解的bean
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
// 最后,調(diào)用其他的BeanDefinitionRegistryPostProcessor的方法
boolean reiterate = true;
while (reiterate) {
reiterate = false;
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
reiterate = true;
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
//調(diào)用其他BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry方法
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
}
//調(diào)用BeanDefinitionRegistryPostProcessor的postProcessBeanFactory
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
//方法傳入普通BeanFactoryPostProcessor, 實(shí)現(xiàn)其postProcessBeanFactory方法
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
}
else {
//如果beanFactory不是BeanDefinitionRegistry
//方法傳入普通BeanFactoryPostProcessor, 實(shí)現(xiàn)其postProcessBeanFactory方法
invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}
//處理beanFactory中注冊(cè)的普通BeanFactoryPostProcessor
//非BeanDefinitionRegistryPostProcessor類型后置處理器
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
if (processedBeans.contains(ppName)) {
//說明已經(jīng)處理過了
}
else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
//優(yōu)先處理實(shí)現(xiàn)了PriorityOrdered接口的子類
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
//再處理實(shí)現(xiàn)了Ordered接口的子類
orderedPostProcessorNames.add(ppName);
}
else {
//最后處理其他BeanFactoryPostProcessor
nonOrderedPostProcessorNames.add(ppName);
}
}
//實(shí)現(xiàn)了PriorityOrdered接口的BeanFactoryPostProcessor, 優(yōu)先處理
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
// 實(shí)現(xiàn)了Ordered接口的BeanFactoryPostProcessor
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>();
for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(orderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
// 最后再實(shí)現(xiàn)不排序BeanFactoryPostProcessor
List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
for (String postProcessorName : nonOrderedPostProcessorNames) {
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
// 清除緩存
beanFactory.clearMetadataCache();
}
}
8.注冊(cè)bean處理器 registerBeanPostProcessors(beanFactory)
BeanPostProcessor是一個(gè)接口, Bean后置處理器, 在bean實(shí)例化, 之前執(zhí)行postProcessBeforeInitialization方法, 在bean實(shí)例化之后執(zhí)行postProcessAfterInitialization方法, 實(shí)現(xiàn)了對(duì)bean實(shí)例的增強(qiáng)
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {
//注冊(cè)beanPostProcessors
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}
}
PostProcessorRegistrationDelegate.registerBeanPostProcessors
beanFactory中獲取BeanPostProcessor類型的bean名稱, 維護(hù)到postProcessorNames數(shù)組中, 將BeanPostProcessor列表分為四類:
- 實(shí)現(xiàn)了PriorityOrdered接口, 維護(hù)到priorityOrderedPostProcessors列表中
- 實(shí)現(xiàn)了Ordered接口的BeanPostProcessor, 維護(hù)到orderedPostProcessorNames列表中
- 沒有實(shí)現(xiàn)排序接口的, 維護(hù)到nonOrderedPostProcessorNames列表中
- 實(shí)現(xiàn)了MergedBeanDefinitionPostProcessor接口, 維護(hù)到internalPostProcessors列表中
beanFactory先添加一個(gè)BeanPostProcessorChecker類型的BeanPostProcessor, 然后在將各類PostProcessors列表排序, 分別添加到beanFactory的beanPostProcessor列表中, 最后再添加一個(gè)ApplicationListenerDetector
final class PostProcessorRegistrationDelegate {
public static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
// 添加一個(gè)BeanPostProcessorChecker
// beanFactory數(shù)量不符合時(shí),會(huì)打印info日志
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
// BeanPostProcess分類
// 實(shí)現(xiàn)了PriorityOrdered接口
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
//實(shí)現(xiàn)了MergedBeanDefinitionPostProcessor接口
List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
// 實(shí)現(xiàn)了Ordered接口
List<String> orderedPostProcessorNames = new ArrayList<>();
// 沒有實(shí)現(xiàn)排序接口
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
//PriorityOrdered子類
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
priorityOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
//MergedBeanDefinitionPostProcessor子類
internalPostProcessors.add(pp);
}
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
// Ordered子類
orderedPostProcessorNames.add(ppName);
}
else {
// 普通BeanPostProcessor
nonOrderedPostProcessorNames.add(ppName);
}
}
// 實(shí)現(xiàn)了PriorityOrdered接口的BeanPostProcessor, 先排序
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
// 然后添加到beanFactory中
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
// 實(shí)現(xiàn)了Ordered接口的BeanPostProcessor
List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
for (String ppName : orderedPostProcessorNames) {
// 先從beanFactory中獲取bean
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
// 然后添加到orderedPostProcessors列表中
orderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
// 如果是MergedBeanDefinitionPostProcessor的子類
// 那么加入internalPostProcessors中
internalPostProcessors.add(pp);
}
}
// 排序
sortPostProcessors(orderedPostProcessors, beanFactory);
// 添加到beanFactory中
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
// 添加其他BeanPostProcessor
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
for (String ppName : nonOrderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
nonOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
// 如果是MergedBeanDefinitionPostProcessor的子類
// 那么加入internalPostProcessors中
internalPostProcessors.add(pp);
}
}
// 添加到beanFactory中
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
// 最后internalPostProcessors排序
// 添加到beanFactory中
sortPostProcessors(internalPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, internalPostProcessors);
// 添加一個(gè)ApplicationListenerDetector的beanFactory
// 處理ApplicationListener類型的bean
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
}
9.國(guó)際化 initMessageSource()
先判斷容器beanFactory中是否包含messageSource bean定義, 存在的話, 直接獲取bean, 如果不存在的話, 那么手工注冊(cè)一個(gè)messageSource單例bean, 然后賦值給this.messageSource
protected void initMessageSource() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
//判斷容器中是否包含messageSource bean定義
if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
//容器中存在bean定義, 那么直接獲取bean
this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
//HierarchicalMessageSource可以實(shí)現(xiàn)消息的嵌套
HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
if (hms.getParentMessageSource() == null) {
hms.setParentMessageSource(getInternalParentMessageSource());
}
}
if (logger.isTraceEnabled()) {
logger.trace("Using MessageSource [" + this.messageSource + "]");
}
}
else {
//容器中沒有messageSource bean定義
DelegatingMessageSource dms = new DelegatingMessageSource();
dms.setParentMessageSource(getInternalParentMessageSource());
//手工注冊(cè)一個(gè)單例messageSource bean
this.messageSource = dms;
beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
if (logger.isTraceEnabled()) {
logger.trace("No '" + MESSAGE_SOURCE_BEAN_NAME + "' bean, using [" + this.messageSource + "]");
}
}
}
10. 注冊(cè)事件發(fā)布器 initApplicationEventMulticaster()
先判斷容器beanFactory中是否有applicationEventMulticaster bean定義, 存在的話, 獲取bean實(shí)例, 不存在的話, 實(shí)例化一個(gè)SimpleApplicationEventMulticaster, 手工注冊(cè)一個(gè)單例bean, 然后賦值給this.applicationEventMulticaster
protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
//先判斷容器beanFactory中是否有applicationEventMulticaster bean定義
if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
//存在的話, 獲取bean實(shí)例
this.applicationEventMulticaster =
beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
if (logger.isTraceEnabled()) {
logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
}
else {
//容器中不存在的話, 實(shí)例化一個(gè)SimpleApplicationEventMulticaster
//手工注冊(cè)一個(gè)單例bean
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
if (logger.isTraceEnabled()) {
logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
"[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
}
}
}
11. 通知子類刷新刷新容器 onRefresh()
AbstractApplicationContext沒有實(shí)現(xiàn)該方法, 用于通知子類刷新容器
ServletWebServerApplicationContext
調(diào)用父類GenericWebApplicationContext#onRefresh方法, 然后創(chuàng)建webServer, 之后調(diào)用父類GenericWebApplicationContext#initPropertySources方法, 將servletContext維護(hù)到environment的servletContextInitParams屬性中
public class ServletWebServerApplicationContext extends GenericWebApplicationContext
implements ConfigurableWebServerApplicationContext {
@Override
protected void onRefresh() {
//調(diào)用父類onRefresh方法
super.onRefresh();
try {
//創(chuàng)建webServer
createWebServer();
}
catch (Throwable ex) {
throw new ApplicationContextException("Unable to start web server", ex);
}
}
//創(chuàng)建webServer
private void createWebServer() {
WebServer webServer = this.webServer;
//默認(rèn)servletContext為null
ServletContext servletContext = getServletContext();
if (webServer == null && servletContext == null) {
//獲取一個(gè)ServletWebServerFactory
ServletWebServerFactory factory = getWebServerFactory();
//factory獲取一個(gè)webServer
this.webServer = factory.getWebServer(getSelfInitializer());
}
else if (servletContext != null) {
try {
//servletContext不為空
getSelfInitializer().onStartup(servletContext);
}
catch (ServletException ex) {
throw new ApplicationContextException("Cannot initialize servlet context",
ex);
}
}
//初始化
initPropertySources();
}
}
GenericWebApplicationContext
初始化主題, 可以讓頁(yè)面顯示不同的樣式
public class GenericWebApplicationContext extends GenericApplicationContext
implements ConfigurableWebApplicationContext, ThemeSource {
@Override
protected void onRefresh() {
//初始化資源
this.themeSource = UiApplicationContextUtils.initThemeSource(this);
}
}
12.注冊(cè)監(jiān)聽器 registerListeners()
首先將硬編碼的ApplicationListener先添加this.applicationEventMulticaster.defaultRetriever.applicationListeners中, 然后將注入的listener bean維護(hù)到this.applicationEventMulticaster.defaultRetriever.applicationListenerBeans, 最后處理earlyEvent
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {
protected void registerListeners() {
// 首先將硬編碼的ApplicationListener先添加applicationEventMulticaster中
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
// 然后將注入的listener bean維護(hù)到applicationEventMulticaster中
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// 處理earlyEvent
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
}
13. 完成beanFactory初始化, 實(shí)例化所有非延遲加載的bean
beanFactory.preInstantiateSingletons() 源碼剖析
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
// 初始化ConversionService的實(shí)例化
// 然后賦值給beanFactory.conversionService實(shí)例
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}
// 如果beanFactory中沒有@Value解析器
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
// 類加載期間織入的切面
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
//實(shí)例化所有LoadTimeWeaverAware類型的bean
getBean(weaverAwareName);
}
//停止使用臨時(shí)ClassLoader
beanFactory.setTempClassLoader(null);
//凍結(jié)配置,不再允許改變
beanFactory.freezeConfiguration();
//實(shí)例化所有非延遲加載的bean
beanFactory.preInstantiateSingletons();
}
}
14.完成刷新 finishRefresh()
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {
protected void finishRefresh() {
// 清空緩存
clearResourceCaches();
// 初始化一個(gè)LifecycleProcessor
// 在Spring啟動(dòng)的時(shí)候啟動(dòng)bean
// 在spring結(jié)束的時(shí)候銷毀bean
initLifecycleProcessor();
// 調(diào)用LifecycleProcessor的onRefresh方法
// 啟動(dòng)實(shí)現(xiàn)了Lifecycle接口的bean
getLifecycleProcessor().onRefresh();
// 發(fā)布ContextRefreshedEvent
publishEvent(new ContextRefreshedEvent(this));
// 如果配置了"spring.liveBeansView.mbeanDomain"的系統(tǒng)屬性,制定了MBeanServer,
// 那么會(huì)將ApplicationContext注冊(cè)到MBeanServer中,
// 可以通過MBeanServer,對(duì)spring容器中的bean,進(jìn)行實(shí)時(shí)的查看和管理
LiveBeansView.registerApplicationContext(this);
}
}
發(fā)布ContextRefreshedEvent
DelegatingApplicationListener
什么都沒有做
ConditionEvaluationReportListener
打印@Condition注解評(píng)估日志
public class ConditionEvaluationReportLoggingListener
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
protected void onApplicationEvent(ApplicationEvent event) {
ConfigurableApplicationContext initializerApplicationContext = this.applicationContext;
if (event instanceof ContextRefreshedEvent) {
//打印刷新成功日志
if (((ApplicationContextEvent) event)
.getApplicationContext() == initializerApplicationContext) {
logAutoConfigurationReport();
}
}
else if (event instanceof ApplicationFailedEvent
&& ((ApplicationFailedEvent) event)
.getApplicationContext() == initializerApplicationContext) {
//打印失敗日志
logAutoConfigurationReport(true);
}
}
}
ClearCachesApplicationListener
清空ReflectionUtils的緩存, 遞歸清空classLoader緩存
class ClearCachesApplicationListener
implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ReflectionUtils.clearCache();
clearClassLoaderCaches(Thread.currentThread().getContextClassLoader());
}
}
SharedMetadataReaderFactoryBean
static class SharedMetadataReaderFactoryBean
implements FactoryBean<ConcurrentReferenceCachingMetadataReaderFactory>,
BeanClassLoaderAware, ApplicationListener<ContextRefreshedEvent> {
public void onApplicationEvent(ContextRefreshedEvent event) {
//清空metadataReaderFactory的緩存
this.metadataReaderFactory.clearCache();
}
}
ResourceUrlProvider
public class ResourceUrlProvider implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
//默認(rèn)為true
if (isAutodetect()) {
//自動(dòng)推斷pattern對(duì)應(yīng)的ResourceHttpRequestHandler
this.handlerMap.clear();
detectResourceHandlers(event.getApplicationContext());
if (!this.handlerMap.isEmpty()) {
this.autodetect = false;
}
}
}
}
15. 異常處理
//beanFactory銷毀注冊(cè)的bean
destroyBeans();
//ApplicationContext取消刷新
cancelRefresh(ex);
16. 清除緩存 resetCommonCaches()
protected void resetCommonCaches() {
//反射工具類的緩存清空
ReflectionUtils.clearCache();
//注解工具類的緩存清空
AnnotationUtils.clearCache();
//ResolvableType的緩存清空
ResolvableType.clearCache();
//清空ClassLoader
CachedIntrospectionResults.clearClassLoader(getClassLoader());
}
總結(jié)
本文主要分析了spring容器的刷新, 首先更新刷新狀態(tài), 然后處理beanFactory的后置處理器, 用于注冊(cè)bean定義, 其中ConfigurationClassPostProcessor處理器, 處理模塊中@Configuration注解, onRefresh()方法中, 實(shí)例化了TomcatWebServer, 最后在finishRefresh()中, 實(shí)例化了所有bean
下一篇
我們將會(huì)在下一篇啟動(dòng)完成, 研究springboot啟動(dòng)完成的操作