ClassPathXmlApplicationContext源码分析

ClassPathXmlApplicationContext源码解读

入口函数

通过ClassPathXmlApplicationContext类构造方法启动父类AbstractApplicationContext的函数refresh方法

ClassPathXmlApplicationContext源码:

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) 一般使用这个方法,传入一个string类型的xml文件名,通过xml配置文件指定特定的组件,实例化到bean工厂里,即DefaultSingletonBeanRegistry 下的Map集合里

ClassPathXmlApplicationContext(String... configLocations):可传入多个xml,装配到spring,bean工厂

public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) :传入String数组

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

 

ClassPathXmlApplicationContext源码分析

上一篇:iOS7时代我们用什么来追踪和识别用户?


下一篇:Android 开源框架Universal-Image-Loader完全解析(二)--- 图片缓存策略详解