Spring源码阅读-ClassPathXmlApplicationContext

第一步:new一个ClassPathXmlApplicationContext对象

复制代码
ClassPathXmlApplicationContext xmlContext = new ClassPathXmlApplicationContext("mylearn.xml");

第二步:调用构造方法

复制代码
public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
   this(new String[] {configLocation}, true, null);
}
复制代码
public ClassPathXmlApplicationContext(
      String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
      throws BeansException {

   super(parent);
   //设置上下文的配置路径
   setConfigLocations(configLocations);
   if (refresh) {
      //刷新上下文
      refresh();
   }
}
复制代码
public void setConfigLocations(@Nullable 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;
   }
}
复制代码
@Override
public void refresh() throws BeansException, IllegalStateException {
   synchronized (this.startupShutdownMonitor) {
      StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");

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

         StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
         // Invoke factory processors registered as beans in the context.
         invokeBeanFactoryPostProcessors(beanFactory);

         // Register bean processors that intercept bean creation.
         registerBeanPostProcessors(beanFactory);
         beanPostProcess.end();

         // 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();
         contextRefresh.end();
      }
   }
}

第三步:调用抽象父类AbstractXmlApplicationContext

复制代码
public AbstractXmlApplicationContext(@Nullable ApplicationContext parent) {
   super(parent);
}

第四步:调用抽象父类AbstractRefreshableConfigApplicationContext

复制代码
public AbstractRefreshableConfigApplicationContext(@Nullable ApplicationContext parent) {
   super(parent);
}

第五步:调用抽象父类AbstractRefreshableApplicationContext

复制代码
public AbstractRefreshableConfigApplicationContext(@Nullable ApplicationContext parent) {
   super(parent);
}

第六步:调用抽象父类AbstractApplicationContext

复制代码
public AbstractApplicationContext(@Nullable ApplicationContext parent) {
   this();
   setParent(parent);
}
复制代码
public AbstractApplicationContext() {
   this.resourcePatternResolver = getResourcePatternResolver();
}

获取接口解析器 ResourcePatternResolver

复制代码
protected ResourcePatternResolver getResourcePatternResolver() {
   return new PathMatchingResourcePatternResolver(this);
}

第七步:设置应用上下文接口ApplicationContext

复制代码
@Override
public void setParent(@Nullable ApplicationContext parent) {
   this.parent = parent;
   if (parent != null) {
      Environment parentEnvironment = parent.getEnvironment();
      if (parentEnvironment instanceof ConfigurableEnvironment) {
         getEnvironment().merge((ConfigurableEnvironment) parentEnvironment);
      }
   }
}
相关推荐
不学会Ⅳ11 分钟前
Mac M芯片搭建jdk源码环境(jdk24)
java·开发语言·macos
虫小宝15 分钟前
高佣金返利平台监控体系建设:APM、链路追踪与佣金异常预警系统技术实现
java
sniper_fandc1 小时前
SpringBoot系列—入门
java·spring boot·后端
代码的余温2 小时前
Maven引入第三方JAR包实战指南
java·maven·jar
先睡4 小时前
Redis的缓存击穿和缓存雪崩
redis·spring·缓存
pianmian16 小时前
类(JavaBean类)和对象
java
我叫小白菜6 小时前
【Java_EE】单例模式、阻塞队列、线程池、定时器
java·开发语言
Albert Edison7 小时前
【最新版】IntelliJ IDEA 2025 创建 SpringBoot 项目
java·spring boot·intellij-idea
超级小忍7 小时前
JVM 中的垃圾回收算法及垃圾回收器详解
java·jvm
weixin_446122467 小时前
JAVA内存区域划分
java·开发语言·redis