ClassPathXmlApplicationContext source code analysis

ClassPathXmlApplicationContext source Interpretation

Entry function

By ClassPathXmlApplicationContextstarting parent class constructor AbstractApplicationContextfunction refresh method

ClassPathXmlApplicationContextSource:

package org.springframework.context.support;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext {
  @Nullable
  private Resource[] configResources;

  public ClassPathXmlApplicationContext() {
  }

  public ClassPathXmlApplicationContext(ApplicationContext parent) {
      super(parent);
  }

  public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
      this(new String[]{configLocation}, true, (ApplicationContext)null);
  }

  public ClassPathXmlApplicationContext(String... configLocations) throws BeansException {
      this(configLocations, true, (ApplicationContext)null);
  }

  public ClassPathXmlApplicationContext(String[] configLocations, @Nullable ApplicationContext parent) throws BeansException {
      this(configLocations, true, parent);
  }

  public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {
      this(configLocations, refresh, (ApplicationContext)null);
  }

  public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {
      super(parent);
      this.setConfigLocations(configLocations);
      if (refresh) {
          this.refresh();
      }

  }

  public ClassPathXmlApplicationContext(String path, Class<?> clazz) throws BeansException {
      this(new String[]{path}, clazz);
  }

  public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz) throws BeansException {
      this(paths, clazz, (ApplicationContext)null);
  }

  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);
      }

      this.refresh();
  }

  @Nullable
  protected Resource[] getConfigResources() {
      return this.configResources;
  }
}

ClassPathXmlApplicationContext(String configLocation) Using this general method, passing a string type xml file name, to specify a particular component through xml configuration file to instantiate the bean plant, i.e. the set of the Map in DefaultSingletonBeanRegistry

ClassPathXmlApplicationContext(String... configLocations):It may be a plurality of incoming xml, fitted into the spring, bean plants

public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) :Incoming String array

@Test
  public void TestXmlBean(){
      ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("classpath:xmlRegBean.xml");
      Animal cat =context.getBean("cat", Cat.class);
      cat.getName();
  }

  @Test
  public void TestMultiXml(){
          ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("classpath:xmlRegBean.xml","classpath:xmlRegBean2.xml");
          Animal cat =context.getBean("cat", Cat.class);
          cat.getName();
          System.out.println(cat);
          Animal cat2 =context.getBean("cat2", Cat.class);
          cat2.getName();
          System.out.println(cat2);
  }
   
@Test
  public void TestArrayXml(){
      String[] arrXml = {"classpath:xmlRegBean.xml","classpath:xmlRegBean2.xml"};
      ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext(arrXml,true);
      Animal cat =context.getBean("cat", Cat.class);
      cat.getName();
      System.out.println(cat);
      Animal cat2 =context.getBean("cat2", Cat.class);
      cat2.getName();
      System.out.println(cat2);
  }

 

Guess you like

Origin www.cnblogs.com/nicklin/p/12095885.html