讲完上一节的spring boot的启动流程,这一节主要讲一下上一节没有具体讲的refresh context部分,这部分的代码实现是在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) {
}
finally {
}
}
}
说一下这个方法主要的步骤。
1. obtainFreshBeanFactory()
获取之前在创建ApplicationContext时创建的DefaultListableBeanFactory对象,然后设置DefaultListableBeanFactory的serializationId属性。
2. prepareBeanFactory()
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// Tell the internal bean factory to use the context's class loader etc.
beanFactory.setBeanClassLoader(getClassLoader());
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
// Configure the bean factory with context callbacks.
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
// BeanFactory interface not registered as resolvable type in a plain factory.
// MessageSource registered (and found for autowiring) as a bean.
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
// Register early post-processor for detecting inner beans as ApplicationListeners.
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
// Detect a LoadTimeWeaver and prepare for weaving, if found.
if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
// Set a temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
// Register default environment beans.
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());
}
}
(1)设置StandardBeanExpressionResolver解析器,用以解析spring的EL表达式,后面使用时具体说。
(2)增加了一个默认的propertyEditor。
(3)注册一个ApplicationContextAwareProcessor处理器,在这个beanPostProcessor处理器中,处理EnvironmentAware、EmbeddedValueResolverAware等实现Aware接口的实现类。
(4)上面的处理器已经处理了实现Aware接口的实现类,这里把这些处理类忽略,关于Aware接口的用法参考这篇Aware接口。
(5)注册依赖,当检查到依赖时,将依赖的实例进行注册。
(6)注册ApplicationListenerDetector处理器,用于检测加载的bean中是否有ApplicationListener子类,有的话加入此监听器。
(7)剩下的几步没看太明白,后面理解了再补充。
3. postProcessBeanFactory
@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
super.postProcessBeanFactory(beanFactory);
if (this.basePackages != null && this.basePackages.length > 0) {
this.scanner.scan(this.basePackages);
}
if (this.annotatedClasses != null && this.annotatedClasses.length > 0) {
this.reader.register(this.annotatedClasses);
}
}
@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
beanFactory.addBeanPostProcessor(
new WebApplicationContextServletContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(ServletContextAware.class);
}
(1) 增加了一个BeanPostProcessor处理类WebApplicationContextServletContextAwareProcessor,这个处理类的作用主要是处理实现ServletContextAware接口的bean,在这个处理类,初始化这个bean中的ServletContext对象,这样在实现ServletContextAware接口的bean中就可以拿到ServletContext对象了,Spring中Aware接口就是这样实现的。
(2) 忽略ServletContextAware接口,因为实现ServletContextAware接口的bean在第一步中的WebApplicationContextServletContextAwareProcessor中已经处理了。
4. invokeBeanFactoryPostProcessors
这个方法主要就是加载项目中的bean到DefaultListableBeanFactory中的beanDefinitionMap中,具体过程下节具体分析。
5. registerBeanPostProcessors
public static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
// Register BeanPostProcessorChecker that logs an info message when
// a bean is created during BeanPostProcessor instantiation, i.e. when
// a bean is not eligible for getting processed by all BeanPostProcessors.
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
// Separate between BeanPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
List<BeanPostProcessor> internalPostProcessors = new ArrayList<BeanPostProcessor>();
List<String> orderedPostProcessorNames = new ArrayList<String>();
List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
priorityOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
else {
nonOrderedPostProcessorNames.add(ppName);
}
}
// First, register the BeanPostProcessors that implement PriorityOrdered.
sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
// Next, register the BeanPostProcessors that implement Ordered.
List<BeanPostProcessor> orderedPostProcessors = new ArrayList<BeanPostProcessor>();
for (String ppName : orderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
orderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
sortPostProcessors(beanFactory, orderedPostProcessors);
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
// Now, register all regular BeanPostProcessors.
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
for (String ppName : nonOrderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
nonOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
// Finally, re-register all internal BeanPostProcessors.
sortPostProcessors(beanFactory, internalPostProcessors);
registerBeanPostProcessors(beanFactory, internalPostProcessors);
// Re-register post-processor for detecting inner beans as ApplicationListeners,
// moving it to the end of the processor chain (for picking up proxies etc).
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
上一步已经加载项目中的所有bean到DefaultListableBeanFactory中的beanDefinitionMap中,这一步主要是扫描这个map中的bean,如果是BeanPostProcessor的子类,则将这些BeanPostProcessor类加载到DefaultListableBeanFactory中,具体分析过程如下:
(1)通过beanFactory.getBeanNamesForType获取BeanPostProcessor的所有实现类。
(2)增加一个BeanPostProcessorChecker处理器类,这个处理器中会校验beanFactory中注册的BeanPostProcessor的个数是否和beanProcessorTargetCount这个值相同。
(3)获取实现了PriorityOrdered接口的所有BeanPostProcessor子类,然后对这些BeanPostProcessor子类进行排序,排序完成后将这些类按照顺序注册到DefaultListableBeanFactory中。
(4)获取实现了Ordered接口的所有BeanPostProcessor子类,然后对这些BeanPostProcessor子类进行排序,排序完成后将这些类按照顺序注册到DefaultListableBeanFactory中。
(5)将既没有实现PriorityOrdered接口又没有实现Ordered接口的BeanPostProcessor子类注册到DefaultListableBeanFactory中。
(6)最后将ApplicationListenerDetector这个负责扫描发现监听器子类的处理器放到最后一个处理器。
经过上面这些步骤,BeanPostProcessor就按照顺序添加到了DefaultListableBeanFactory,后面在初始化bean实例时,就按照这个添加顺序来处理bean。
6. initMessageSource
消息格式化处理,具体还没有研究。
7. initApplicationEventMulticaster
protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
this.applicationEventMulticaster =
beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
if (logger.isDebugEnabled()) {
logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
}
else {
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
if (logger.isDebugEnabled()) {
logger.debug("Unable to locate ApplicationEventMulticaster with name '" +
APPLICATION_EVENT_MULTICASTER_BEAN_NAME +
"': using default [" + this.applicationEventMulticaster + "]");
}
}
}
在DefaultListableBeanFactory中注册了SimpleApplicationEventMulticaster这个bean,SimpleApplicationEventMulticaster这个类是一个事件广播器,完成将具体事件通知到负责监听此事件的监听器,对spring事件处理不熟悉可以参考这篇文章spring事件广播器。
8. finishBeanFactoryInitialization
这一步也比较重要,将保存在DefaultListableBeanFactory中的beanDefinitionMap中完成实例化,这一块也放在下一节讲。
9. finishRefresh
这一步主要是发布context刷新完成事件,通知相应的监听器进行处理。
到这里,ApplicationContext刷新就讲完了,下一节就开始讲本节没有讲的bean加载和创建部分。