前言
在Spring框架中要说最重要的方法就是AbstrctApplicationContext.refresh()方法,在这个方法中完成对容器的刷新和bean对象的加载过程,下面就详细对这个方法进行分析;
refresh()方法概述
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
//1.1 准备此上下文以进行刷新.
prepareRefresh();
//2.1 告诉子类刷新内部bean工厂.(获取beanFactory)
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
//2.2 准备在这种情况下使用的bean工厂.(对beanFactory进行预处理)
prepareBeanFactory(beanFactory);
try {
//2.3 在上下文子类中对bean工厂进行后置处理.
postProcessBeanFactory(beanFactory);
//3.1 调用在上下文中注册为Bean的工厂处理器.
invokeBeanFactoryPostProcessors(beanFactory);
//3.2 注册拦截Bean创建的Bean处理器.
registerBeanPostProcessors(beanFactory);
//4.1 为此上下文初始化消息源.
initMessageSource();
//4.2 为此上下文初始化事件广播器.
initApplicationEventMulticaster();
//5.1 在特定上下文子类中初始化其他特殊bean.
onRefresh();
//6 检查侦听器bean并注册它们.
registerListeners();
//7 实例化所有剩余的(非延迟初始化)单例bean.
finishBeanFactoryInitialization(beanFactory);
//8 最后一步:发布相应事件.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// 销毁已创建的单例以避免资源悬空.
destroyBeans();
// 重置“active”标志
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
//自检并重置spring中缓存的bean的元数据,因为可能不在使用
resetCommonCaches();
}
}
}
通过源代码可以看到AbstrctApplicationContext.refresh()方法分为8个步骤
- 准备环境上下文
- 创建准备beanFactory
- 处理前置扩展点(BeanFactoryPostProcessor/BeanPostProcessor)
- 事件广播
- onRefresh
- 注册监听器
- 实例化剩下的bean
- 发布事件
下面根据步骤进一步分析refresh()方法的具体操作;
准备环境上下文
配置应用刷新上下文环境的方法主要有prepareRefresh()、obtainFreshBeanFactory()、prepareBeanFactory(beanFactory)
prepareRefresh()
/*
* 准备此上下文以进行刷新,设置其启动日期和
* 活动标志,以及执行属性源的任何初始化.
*/
protected void prepareRefresh() {
//切换到活动状态.
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());
}
}
// 初始化ProperySources容器.
initPropertySources();
// 验证所有标记为必需的属性都是可解析的:
// see ConfigurablePropertyResolver#setRequiredProperties
getEnvironment().validateRequiredProperties();
// ApplicationListeners设置为预处理
if (this.earlyApplicationListeners == null) {
this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
}
else {
// 将ApplicationListeners重新设置成预处理状态.
this.applicationListeners.clear();
this.applicationListeners.addAll(this.earlyApplicationListeners);
}
prepareRefresh()方法只是在清理上下文环境、设置启动事件以及初始化PropertySources容器等;
初始化PropertySources容器方法对应web应用底层是调用
WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig)
实现将servletContext, servletConfig加载到PropertySources容器中的
- servletContext是web应用服务器为每一个应用创建的一个服务上下文对象
- servletConfig是操作一个servlet中的配置属性的接口
创建准备beanFactory
obtainFreshBeanFactory()
obtainFreshBeanFactory()
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory();
return getBeanFactory();
}
- refreshBeanFactory()刷新beanFactory,底层是调用DefaultListableBeanFactory.setSerializationId()方法去刷新工厂集合
//这里使用的是ConcurrentHashMap
private static final Map<String, Reference<DefaultListableBeanFactory>> serializableFactories =
new ConcurrentHashMap<>(8);
public void setSerializationId(@Nullable String serializationId) {
if (serializationId != null) {
//注意这里使用的是WeakReference保证了并发安全性
serializableFactories.put(serializationId, new WeakReference<>(this));
}
else if (this.serializationId != null) {
serializableFactories.remove(this.serializationId);
}
this.serializationId = serializationId;
}
- getBeanFactory()方法不同的实现都是返回DefaultListableBeanFactory
prepareBeanFactory(beanFactory)
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// 告诉内部bean工厂使用上下文的类加载器等.
beanFactory.setBeanClassLoader(getClassLoader());
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
// 使用上下文回调配置Bean工厂.
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接口不在普通工厂中注册设置为可解析类型.
// MessageSource注册为Bean(并发现用于自动装配).
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
// 注册早期的后置处理器以将内部bean检测为ApplicationListeners.
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
// 如果发现LoadTimeWeaver,请准备织入。
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()));
}
// 注册默认环境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());
}
}
prepareBeanFactory(beanFactory)主要是给beanFactory设置各种预设的属性的
小结
在开始加载bean之前需要做三件事情:
- 清理环境,设置启动时间,装载servletContext和servletConfig
- 获取DefaultListableBeanFactory
- 给获取到的DefaultListableBeanFactory设置各种属性
处理前置扩展点
postProcessBeanFactory是用来在应用上下文中注册一些特殊的BeanPostProrecessors
GenericWebApplicationContext分析
- 代码展示
@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
if (this.servletContext != null) {
beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext));
beanFactory.ignoreDependencyInterface(ServletContextAware.class);
}
WebApplicationContextUtils.registerWebApplicationScopes(beanFactory, this.servletContext);
WebApplicationContextUtils.registerEnvironmentBeans(beanFactory, this.servletContext);
}
- ServletContextAwareProcessor
- WebApplicationContextUtils.registerWebApplicationScopes(beanFactory, this.servletContext)
public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory,
@Nullable ServletContext sc) {
beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope());
if (sc != null) {
ServletContextScope appScope = new ServletContextScope(sc);
beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
// Register as ServletContext attribute, for ContextCleanupListener to detect it.
sc.setAttribute(ServletContextScope.class.getName(), appScope);
}
beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());
beanFactory.registerResolvableDependency(ServletResponse.class, new ResponseObjectFactory());
beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());
beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
if (jsfPresent) {
FacesDependencyRegistrar.registerFacesDependencies(beanFactory);
}
}
- 主要是用于注册web应用程序bean的注册范围增加request、session、application
- WebApplicationContextUtils.registerEnvironmentBeans(beanFactory, this.servletContext)
public static void registerEnvironmentBeans(ConfigurableListableBeanFactory bf,
@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) {
if (servletContext != null && !bf.containsBean(WebApplicationContext.SERVLET_CONTEXT_BEAN_NAME)) {
bf.registerSingleton(WebApplicationContext.SERVLET_CONTEXT_BEAN_NAME, servletContext);
}
if (servletConfig != null && !bf.containsBean(ConfigurableWebApplicationContext.SERVLET_CONFIG_BEAN_NAME)) {
bf.registerSingleton(ConfigurableWebApplicationContext.SERVLET_CONFIG_BEAN_NAME, servletConfig);
}
//省略...
bf.registerSingleton(WebApplicationContext.CONTEXT_PARAMETERS_BEAN_NAME,
Collections.unmodifiableMap(parameterMap));
}
//省略...
bf.registerSingleton(WebApplicationContext.CONTEXT_ATTRIBUTES_BEAN_NAME,
Collections.unmodifiableMap(attributeMap));
}
}
- 将servletContext、servletConfig、contextParameters、contextParameters注册到容器中
postProcessBeanFactory方法就是在bean初始化之前将一些特殊的bean注册到容器中去,从而完成接下来的初始化操作
invokeBeanFactoryPostProcessors
invokeBeanFactoryPostProcessors方法是执行实现BeanFactoryPostProcessor接口的方法
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory,getBeanFactoryPostProcessors());
// Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
// (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
}
getBeanFactoryPostProcessors()中返回的bean是在初始化化环境prepareContext中加载的
invokeBeanFactoryPostProcessors的代码:
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
Set<String> processedBeans = new HashSet<>();
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);
}
else {
regularPostProcessors.add(postProcessor);
}
}
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
//省略....
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
boolean reiterate = true;
while (reiterate) {
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
}
// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
//省略....
}
以上代码的作用就是
- 先处理BeanDefinitionRegistryPostProcessor,按照PriorityOrdered > Ordered > 普通的顺序一次执行postProcessBeanDefinitionRegistry方法;
- 再处理BeanFactoryPostProcessors,按照PriorityOrdered > Ordered > 普通的顺序一次执行postProcessBeanFactory方法;
小结
invokeBeanFactoryPostProcessors方法就是执行实现BeanDefinitionRegistryPostProcessor接口的postProcessBeanDefinitionRegistry以及执行实现BeanFactoryPostProcessors接口的postProcessBeanFactory方法
运行示例
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<>();
List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
// First, register the BeanPostProcessors that implement PriorityOrdered.
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
//省略代码...
// Next, register the BeanPostProcessors that implement Ordered.
List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
sortPostProcessors(orderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
//省略代码...
// Now, register all regular BeanPostProcessors.
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
//省略代码...
// 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));
}
主要是往beanFactory中注册BeanProcessor的实现,在beanFactory中是用CopyOnWriteArrayList来作为缓存容器的。
/** AbstractBeanFactory内部是用CopyOnWriteArrayList来保证安全性的 */
private final List<BeanPostProcessor> beanPostProcessors = new CopyOnWriteArrayList<>();
事件广播
initMessageSource()
initMessageSource是初始化消息源的,也就是进行初始化国际化配置相关的
initApplicationEventMulticaster
initApplicationEventMulticaster进行初始化事件发布器。beanFactory中存在ApplicationEventMulticaster就进行注册,没有就注册SimpleApplicationEventMulticaster。
onRefresh()
onRefresh,用于子类实现容器refresh的过程中执行其他特殊操作,这是一个模板方法,允许子类去扩展
注册监听器
registerListeners注册监听器
protected void registerListeners() {
// 首先注册指定的静态侦听器.
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
// 不要在这里初始化FactoryBeans:我们需要保留所有常规bean
//否则后置处理器将无法对他们进行处理!
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// Publish early application events now that we finally have a multicaster...
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
实例化剩下的bean
finishBeanFactoryInitialization(beanFactory)实列化剩下的bean,这个方法也是绝大多数bean初始化的方式。这个方法的解析放在下一篇文章
发布事件
finishRefresh完成容器的刷新
protected void finishRefresh() {
// 清除上下文级别的资源缓存(例如来自扫描的ASM元数据).
clearResourceCaches();
// 为容器初始化处理器.注册一个LifecycleProcessor
initLifecycleProcessor();
// 对生命周期处理器进行刷新.
getLifecycleProcessor().onRefresh();
// 广播容器初始化完成的消息.
publishEvent(new ContextRefreshedEvent(this));
// 先LiveBeanView中注册ApplicationContext.
LiveBeansView.registerApplicationContext(this);
}
initLifecycleProcessor容器执行容器初始化周期过程中的控制
- 代码运行
总结
- 配置资源初始化环境
- 清理环境,设置启动时间,装载servletContext和servletConfig
- 获取DefaultListableBeanFactory
- 给DefaultListableBeanFactory设置各种属性
- postProcessBeanFactory()
- 注册ServletContextAwareProcessor
- 主要是用于注册web应用程序bean的注册范围增加request、session、application
- 将servletContext、servletConfig、contextParameters、contextParameters注册到容器中
- invokeBeanFactoryPostProcessors()
- 先处理BeanDefinitionRegistryPostProcessor,按照PriorityOrdered > Ordered > 普通的顺序一次执行postProcessBeanDefinitionRegistry方法
- 再处理BeanFactoryPostProcessors,按照PriorityOrdered > Ordered > 普通的顺序一次执行postProcessBeanFactory方法
- registerBeanPostProcessors()
- 往beanFactory中注册BeanProcessor的实现
- initMessageSource进行国际化配置处理
- initApplicationEventMulticaster进行初始化事件发布器。beanFactory中存在ApplicationEventMulticaster就进行注册,没有就注册SimpleApplicationEventMulticaster
- onRefresh,用于子类实现容器refresh的过程中执行其他特殊操作,这是一个模板方法,允许子类去扩展
- registerListeners注册监听器
- finishBeanFactoryInitialization()加载bean
- finishRefresh完成容器的刷新
- 流程图
参考资料