美文网首页
spring 源码学习1

spring 源码学习1

作者: 单传利 | 来源:发表于2018-09-16 23:28 被阅读0次

Spring 定位、加载、注册

首先献上ClassPathXmlApplicationContext类型继承图

重点分析ClassPathXmlApplicationContext classPathXmlApplicationContext =new ClassPathXmlApplicationContext("application.xml") 这句话执行了那些内容

1.首先执行ClassPathXmlApplicationContext

public ClassPathXmlApplicationContext(

String[] configLocations,boolean refresh,@Nullable ApplicationContext parent)

throws BeansException {

super(parent);//设置父类上文,若不存在,则直接跳过

setConfigLocations(configLocations);//根据传入值设置当前配置文件位置  this.configLocations

if (refresh)

{//初始化时 refresh初始值为true

refresh();//那重点来了,这才是最关键方法 初始化上下文最终执 行AbstractApplicationContext.refresh()

}

最终调用AbstractApplicationContext 的refresh()方法

 //容器初始化的过程,读入bean 定义资源,并解析注解

@Override public void refresh() throws BeansException, IllegalStateException {   synchronized (this.startupShutdownMonitor) {//线程安全关键字synchronized       // Prepare this context for refreshing.      prepareRefresh();//定义容器准备刷新的方法 //告诉子类启动refreshBeanFactory方法,Bean定义资源文件的载入从子类的refreshBeanFactory,obtainFreshBeanFactory其实是执行子类AbstractRefreshableApplicationContext的refreshBeanFactory

      // Tell the subclass to refresh the internal bean factory

//解析xml 文件,将xml 保存到DeanDifinition 接下来重点分析obtainFreshBeanFactory      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.

/// 【这里需要知道 BeanFactoryPostProcessor 这个知识点,Bean 如果实现了此接口, // 那么在容器初始化以后,Spring 会负责调用里面的 postProcessBeanFactory 方法。】 // 这里是提供给子类的扩展点,到这里的时候,所有的 Bean 都加载、注册完成了,但是都还没有初始化 // 具体的子类可以在这步的时候根据吱声业务添加或修改一些特殊的 beanFactory属性

         postProcessBeanFactory(beanFactory);         // Invoke factory processors registered as beans in the context.

调用BeanFactoryPostPeocessor各个实现类的postPeocessorBeanFactory(factory)         invokeBeanFactoryPostProcessors(beanFactory);         // Register bean processors that intercept bean creation.

//注册BeanPostProcessor         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.

//设置classloader ,释放配置文件、注册实例化单例类         finishBeanFactoryInitialization(beanFactory);         // Last step: publish corresponding event.         finishRefresh();      }      catch (BeansException ex) {         if (logger.isWarnEnabled()) {            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;      }      finally {         // Reset common introspection caches in Spring's core, since we         // might not ever need metadata for singleton beans anymore...         resetCommonCaches();      }   }}

相关文章

网友评论

      本文标题:spring 源码学习1

      本文链接:https://www.haomeiwen.com/subject/hpdhvxtx.html