美文网首页
一、spring ioc之ClassPathXmlApplica

一、spring ioc之ClassPathXmlApplica

作者: xiaoming_he | 来源:发表于2018-07-01 10:43 被阅读0次

    代码

    ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring-context.xml");
    

    类图

    继承的类图,忽略实现的接口


    image.png

    时序图

    image.png

    详细步骤

    1. new ClassPathXmlApplicationContext(),初始化ClassPathXmlApplicationContext
      public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
              throws BeansException {
          super(parent);
          setConfigLocations(configLocations);
          if (refresh) {
              refresh();
          }
      }
      
    2. 调用AbstractRefreshableConfigApplicationContext的setConfigLocations(configLocations),设置xml文件路径
      public void setConfigLocations(String... locations) {
          if (locations != null) {
              Assert.noNullElements(locations, "Config locations must not be null");
              this.configLocations = new String[locations.length];
              for (int i = 0; i < locations.length; i++) {
                  this.configLocations[i] = resolvePath(locations[i]).trim();
              }
          }
          else {
              this.configLocations = null;
          }
      }
      
    3. 调用AbstractApplicationContext的refresh(),这个方法内容太多,对于ClassPathXmlApplicationContext加载bean,只需了解它的obtainFreshBeanFactory方法。
    4. obtainFreshBeanFactory方法,获取BeanFactory。
    5. 调用AbstractRefreshableApplicationContext的refreshBeanFactory方法
      protected final void refreshBeanFactory() throws BeansException {
          if (hasBeanFactory()) {
              destroyBeans();
              closeBeanFactory();
          }
          try {
              //创建BeanFactory
              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);
          }
      }
      
      protected DefaultListableBeanFactory createBeanFactory() {
          return new DefaultListableBeanFactory(getInternalParentBeanFactory());
      }
      
    6. 调用AbstractXmlApplicationContext的loadBeanDefinitions方法,加载bean
      protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
          // Create a new XmlBeanDefinitionReader for the given BeanFactory.
          XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
      
          // Configure the bean definition reader with this context's
          // resource loading environment.
          beanDefinitionReader.setEnvironment(this.getEnvironment());
          beanDefinitionReader.setResourceLoader(this);
          beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
      
          // Allow a subclass to provide custom initialization of the reader,
          // then proceed with actually loading the bean definitions.
          initBeanDefinitionReader(beanDefinitionReader);
          loadBeanDefinitions(beanDefinitionReader);
      }
      
    7. 加载bean主要是对配置文件的读取,并将配置中的bean读取到DefaultListableBeanFactory中

    相关文章

      网友评论

          本文标题:一、spring ioc之ClassPathXmlApplica

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