AbstractApplicationContext

2024-01-24 10:32

本文主要是介绍AbstractApplicationContext,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

spring初始化Ioc容器很重要的一个方法是由ApplicationContext子接口ConfigurableApplicationContext提供的refresh(),这个方法的作用是创建加载Spring容器配置(包括.xml配置,property文件和数据库模式等)。下面是各个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.  //主要是创建beanFactory,同时加载配置文件.xml中的beanDefinition  //通过String[] configLocations = getConfigLocations()获取资源路径,然后加载beanDefinition  ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();  // Prepare the bean factory for use in this context.  //给beanFactory注册一些标准组建,如ClassLoader,StandardEnvironment,BeanProcess  prepareBeanFactory(beanFactory);  try {  // Allows post-processing of the bean factory in context subclasses.  //提供给子类实现一些postProcess的注册,如AbstractRefreshableWebApplicationContext注册一些Servlet相关的  //postProcess,真对web进行生命周期管理的Scope,通过registerResolvableDependency()方法注册指定ServletRequest,HttpSession,WebRequest对象的工厂方法  postProcessBeanFactory(beanFactory);  // Invoke factory processors registered as beans in the context.  //调用所有BeanFactoryProcessor的postProcessBeanFactory()方法  invokeBeanFactoryPostProcessors(beanFactory);  // Register bean processors that intercept bean creation.  //注册BeanPostProcessor,BeanPostProcessor作用是用于拦截Bean的创建  registerBeanPostProcessors(beanFactory);  // Initialize message source for this context.  //初始化消息Bean  initMessageSource();  // Initialize event multicaster for this context.  //初始化上下文的事件多播组建,ApplicationEvent触发时由multicaster通知给ApplicationListener  initApplicationEventMulticaster();  // Initialize other special beans in specific context subclasses.  //ApplicationContext初始化一些特殊的bean  onRefresh();  // Check for listener beans and register them.  //注册事件监听器,事件监听Bean统一注册到multicaster里头,ApplicationEvent事件触发后会由multicaster广播  registerListeners();  // Instantiate all remaining (non-lazy-init) singletons.  //非延迟加载的单例Bean实例化  finishBeanFactoryInitialization(beanFactory);  // Last step: publish corresponding event.  finishRefresh();  }  catch (BeansException ex) {  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;  }  }  }  

转自http://blog.csdn.net/bubaxiu/article/details/41380683

这篇关于AbstractApplicationContext的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/639405

相关文章

Spring Boot-10-run方法之AbstractApplicationContext#refresh()

本文源码基于Spring 5.2.7 Spring中,容器的填充全靠org.springframework.context.support.AbstractApplicationContext#refresh()方法,这个方法的每一步都是值得分析的,这每一步都会分为一篇或多篇文章来解析,这里对这些步骤作一个整体说明。 Spring的核心能力就是作为控制反转容器使用,通过名称可以看出,这个方法是用来