美文网首页
spring源码分析3----refresh方法

spring源码分析3----refresh方法

作者: 天一阁图书管理员 | 来源:发表于2020-02-20 15:14 被阅读0次

    作者:shihuaping0918@163.com转载请注明作者

    前两篇讲了xml的读取,这一篇继续讲xml的读取是被谁调用的。涉及的类主要是AbstractApplicationContext,以及类中的refresh方法。AbstractApplicationContext这个类代码比较多,挑重点。下面来看一下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.
          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) {
             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();
          }
       }
    }
    

    refresh方法是在ClassPathXmlApplicationContext中被调用的,它做的工作很多,这一篇中先聚焦到xml文件的读取解析和bean注册。后续还会讲到这个方法的。在ClassPathXmlApplicationContext的构造方法中,调用了refresh,代码如下:

    public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz, @Nullable ApplicationContext parent)
          throws BeansException {
    
       super(parent);
       Assert.notNull(paths, "Path array must not be null");
       Assert.notNull(clazz, "Class argument must not be null");
       this.configResources = new Resource[paths.length];
       for (int i = 0; i < paths.length; i++) {
          this.configResources[i] = new ClassPathResource(paths[i], clazz);
       }
       refresh(); //就是这里调的
    }
    

    refresh方法里调了那么多方法,具体是哪一个去做了加载读取xml的工作呢,是obtainFreshBeanFactory这个方法。

    protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
       refreshBeanFactory();
       return getBeanFactory();
    }
    

    obtainFreshBeanFactory这个方法又调了refreshBeanFactory方法,后者是在AbstractRefreshableApplicationContext中实现的。

    @Override
    protected final void refreshBeanFactory() throws BeansException {
       if (hasBeanFactory()) {
          destroyBeans();
          closeBeanFactory();
       }
       try {
          DefaultListableBeanFactory beanFactory = createBeanFactory(); //创建了类厂
          beanFactory.setSerializationId(getId());
          customizeBeanFactory(beanFactory);
          loadBeanDefinitions(beanFactory); //注意这一行
          synchronized (this.beanFactoryMonitor) {
             this.beanFactory = beanFactory; //保存了类厂
          }
       }
       catch (IOException ex) {
          throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
       }
    }
    

    在这个方法中,调用了loadBeanDefinitions,也就是前两篇一直在分析的东西。另外这个方法还创建了类厂,并把它保存起来了。

    在AbstractApplicationContext类中,还有一大块代码是对beanFactory方法加了一层包装,举个例子,从里面随便找一个方法出来。

    @Override
    public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
       assertBeanFactoryActive();
       return getBeanFactory().isSingleton(name);
    }
    

    加这一层实际上是对一些条件进行判定,避免使用时每次都要用户自己去写一遍。

    protected void assertBeanFactoryActive() {
       if (!this.active.get()) {
          if (this.closed.get()) {
             throw new IllegalStateException(getDisplayName() + " has been closed already");
          }
          else {
             throw new IllegalStateException(getDisplayName() + " has not been refreshed yet");
          }
       }
    }
    

    assertBeanFacrotyActive实在是判定beanFacroty是否处于可用状态。this.active和this.closed是两个原子操作类型。它们对应的是这两行代码。

    /** Flag that indicates whether this context is currently active. */
    private final AtomicBoolean active = new AtomicBoolean();
    
    /** Flag that indicates whether this context has been closed already. */
    private final AtomicBoolean closed = new AtomicBoolean();
    
    /*
    

    类型都是AtomicBoolean,原子布尔类型,免加锁。在prepareRefresh方法中,两个变量被设置。

    this.closed.set(false);
    this.active.set(true);
    

    这代表beanFactory已经可用了。

    最后提一下AbstractApplicationContext中出现的ApplicationEvent,spring利用观察者(或者说回调)来实现事件通信,而Event就是用来保存通信内容的。事件通信留待下一篇讲。

    写在最后:接单,有后台活java/cpp/lua/go联系shihuaping0918@163.com。不上班了也要有点收入才行。

    相关文章

      网友评论

          本文标题:spring源码分析3----refresh方法

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