zoukankan      html  css  js  c++  java
  • Spring源码情操陶冶-AbstractApplicationContext

    前言-阅读源码有利于陶冶情操,本文承接前文Spring源码情操陶冶-ContextLoader
    约束:本文指定contextClass为默认的XmlWebApplicationContext

    AbstractApplicationContext#refresh()

    简单看下refresh()方法内罗列的一系列方法,代码清单如下

    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) {
    				// Destroy already created singletons to avoid dangling resources.
    				destroyBeans();
    
    				// Reset 'active' flag.
    				cancelRefresh(ex);
    
    				// Propagate exception to caller.
    				throw ex;
    			}
    		}
    	}
    

    针对上述的代码逻辑,为了避免代码的凌乱以及放在同篇文章文字过长,遂作如下链接导航

    • AbstractApplicationContext#prepareRefresh
      刷新准备工作,@prepareRefresh

    • AbstractApplicationContext#obtainFreshBeanFactory
      涉及解析spring配置文件并封装为BeanDefinition对象保存至beanFactory中,@obtainFreshBeanFactory

    • AbstractApplicationContext#prepareBeanFactory
      beanFactory的准备工作,设置context的属性配置,@prepareBeanFactory

    • AbstractApplicationContext#postProcessBeanFactory
      主要添加ServletContextAwareProcessor处理类,@postProcessBeanFactory

    • AbstractApplicationContext#invokeBeanFactoryPostProcessors
      执行BeanDefinitionRegistryPostProcessors/BeanFactoryPostProcessors相关beans,@invokeBeanFactoryPostProcessors

    • AbstractApplicationContext#registerBeanPostProcessors
      注册所有实现BeanPostProcessor的接口bean到beanFactory的内部属性beanPostProcessors集合中,@registerBeanPostProcessors

    • AbstractApplicationContext#initMessageSource
      初始化资源配置,@initMessageSource

    • AbstractApplicationContext#initApplicationEventMulticaster
      初始化ApplictionEventMulticaster广播事件类,@initApplicationEventMulticaster

    • AbstractApplicationContext#onRefresh
      初始化themeSource,@onRefresh

    • AbstractApplicationContext#registerListeners
      注册ApplicationListener beans到ApplictionEventMulticaster广播集合,@registerListeners

    • AbstractApplicationContext#finishBeanFactoryInitialization
      实例化所有的非lazy-init类型的beans,@finishBeanFactoryInitialization

    • AbstractApplicationContext#finishRefresh
      完成刷新,并执行ContextRefreshedEvent事件,该事件涉及spring mvc,@finishRefresh

    结束语

    阅读源码的目的是为了了解其运行的机制,但可能涵盖的内容过广,可以对有兴趣的地方进行深刻的源码追踪,并理解其中的代码设计,不为是一件快乐的事情

  • 相关阅读:
    SQL Server将一列的多行内容拼接成一行的问题讨论
    SQL 获取 IDENTITY 三种方法 SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY的区别
    构建高性能服务(二)
    乐观锁解决高并发
    c#问答篇:对象与引用变量-----初学者的困惑
    vs调试 本地IIS
    【转】android adb常用指令
    【转】使用 JMeter 完成常用的压力测试
    【转】利用 Apache JMeter 测试 WebSphere 性能
    【转】用JMeter来测试Tomcat的性能
  • 原文地址:https://www.cnblogs.com/question-sky/p/6700089.html
Copyright © 2011-2022 走看看