springboot源码学习笔记系列二:从prepareRefresh到prepareBeanFactory
prepareRefresh
这部分我们先上代码
// Switch to active.
this.startupDate = System.currentTimeMillis();
this.closed.set(false);
this.active.set(true);
if (logger.isDebugEnabled()) {
if (logger.isTraceEnabled()) {
logger.trace("Refreshing " + this);
}
else {
logger.debug("Refreshing " + getDisplayName());
}
}
// Initialize any placeholder property sources in the context environment.
initPropertySources();
// Validate that all properties marked as required are resolvable:
// see ConfigurablePropertyResolver#setRequiredProperties
getEnvironment().validateRequiredProperties();
// Store pre-refresh ApplicationListeners...
if (this.earlyApplicationListeners == null) {
this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
}
else {
// Reset local application listeners to pre-refresh state.
this.applicationListeners.clear();
this.applicationListeners.addAll(this.earlyApplicationListeners);
}
// Allow for the collection of early ApplicationEvents,
// to be published once the multicaster is available...
this.earlyApplicationEvents = new LinkedHashSet<>();
this指的是AbstractApplicationContext这个对象,我们之前的ClassPathXmlApplicationContext对象继承于它,整个的对象关系大概是这样
refresh()方法也是在AbstractApplicationContext,可见这个类还是比较重要的。
this.startupDate = System.currentTimeMillis();
this.closed.set(false);
this.active.set(true);
这些看看名字就大概知道了,记录启动的时间戳,启动开关打开。
之后的
if (logger.isDebugEnabled()) {
if (logger.isTraceEnabled()) {
logger.trace("Refreshing " + this);
}
else {
logger.debug("Refreshing " + getDisplayName());
}
}
是对日志的打印设置,日志等级属于trace和debug等级的,分别打印不同的内容。
// Initialize any placeholder property sources in the context environment.
initPropertySources();
这部分函数默认是空的,不做事,百度翻译了一下,注释的意思是这样的:初始化上下文环境中的任何占位符属性源,可能是环境中可能有些是不太重要的参数的init,忽略。
接下来的
// Validate that all properties marked as required are resolvable:
// see ConfigurablePropertyResolver#setRequiredProperties
getEnvironment().validateRequiredProperties();
是对环境中的需要的参数进行校验,抛出的异常是MissingRequiredPropertiesException,看异常的名字,就是检查下环境中参数是否有缺失。随后做的一步便是对监听器的操作,它把一些在refresh之前的监听器存储进applicationListeners()集合里,猜测方便之后的流程继续使用。最后便是初始化了earlyApplicationEvents,看注释大概和多播器有关,先记在小本本上。至此,prepareRefresh的流程就结束了。整个过程总结下就是,启动开关打开-看看要不要打印日志-占位符参数初始化-检验环境参数-监听器转存和一些有关多播器的初始化。
Bean工厂的创建
这部分我先看了图
refresh中的代码是:
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
可见我们创建的Bean工厂是ConfigurableListableBeanFactory接口类,是BeanFactory的子类,随后我又跟踪了下代码,里面最主要的便是
refreshBeanFactory方法,代码如下:
/**
* This implementation performs an actual refresh of this context's underlying
* bean factory, shutting down the previous bean factory (if any) and
* initializing a fresh bean factory for the next phase of the context's lifecycle.
*/
@Override
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
DefaultListableBeanFactory beanFactory = createBeanFactory();
beanFactory.setSerializationId(getId());
customizeBeanFactory(beanFactory);
loadBeanDefinitions(beanFactory);
this.beanFactory = beanFactory;
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
第一步是销毁原先的bean工厂和bean(如果原先bean工厂就存在的话),之后就是创建了一个DefaultListableBeanFactory 跟踪了下发现是ConfigurableListableBeanFactory的实现类,那合情合理,之后便是给了工厂的序列化id, customizeBeanFactory(beanFactory)进去看了下,是对allowBeanDefinitionOverriding和allowCircularReferences的赋值,前一个是否允许bean的名称相同的进行覆盖,后一个是设置是否允许bean之间的循环引用。这两个设置我们都可以通过自己的方式去修改。后面的loadBeanDefinitions(beanFactory)就是载入BeanDefinitions,debug的时候我也发现,其实代码跑完ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory()的时候,工厂里BeanDefinitions就存在了,可见这步就已经通过BeanDefinitionReader把xml中的bean读取出成BeanDefinition了。
prepareBeanFactory
先贴代码:
/**
* Configure the factory's standard context characteristics,
* such as the context's ClassLoader and post-processors.
* @param beanFactory the BeanFactory to configure
*/
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// Tell the internal bean factory to use the context's class loader etc.
beanFactory.setBeanClassLoader(getClassLoader());
if (!shouldIgnoreSpel) {
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.ignoreDependencyInterface(ApplicationStartup.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 (!IN_NATIVE_IMAGE && 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());
}
if (!beanFactory.containsLocalBean(APPLICATION_STARTUP_BEAN_NAME)) {
beanFactory.registerSingleton(APPLICATION_STARTUP_BEAN_NAME, getApplicationStartup());
}
}
这个prepareBeanFactory总结起来就是设置Bean的类加载器,保证AbstractApplicationContext和Bean使用的是同一个类加载器(这边我个人认为是为了保证Bean的类加载器相同,保证之后的equal等操作能顺利完成),之后的shouldIgnoreSpel是一个boolean型,大概是对是否支持SpEL表达式的设置,之后是添加属性注册编辑器(?记在小本本上)。之后就是把AbstractApplicationContext作为一个BeanPostProcessor加入到工厂中,之后的一大串ignoreDependencyInterface方法是忽略Bean的自动装配,简单点说,就是A包含B,随后如果装配A的时候不去初始化B。后面的registerResolvableDependency方法则是指定实现类,因为可能一个接口有多个实现类,因此这里指定了下具体是哪个实现类,不然会出现异常。随后加入监听器的BeanPostProcessor。之后的LoadTimeWeaver 的是和AOP有关的(在类加载时实现),再记在小本本上。最后的部分便是注册默认的有关环境Bean。