当前位置: 首页 > news >正文

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

http://www.lryc.cn/news/229945.html

相关文章:

  • 考研分享第2期 | 中央财经大学管理科学跨考北大软微金融科技406分经验分享
  • Linux安装java jdk配置环境 方便查询
  • 惊群效应之Nginx处理
  • SpringBoot整合Ldap--超详细方法讲解
  • 【工程实践】Docker使用记录
  • FreeSwitch安装视频
  • SpringBoot3+Vue3+Mysql+Element Plus完成数据库存储blob类型图片,前端渲染后端传来的base64类型图片
  • 攻略 | 参与Moonbeam Ignite Ecosystem Tour
  • 【python自动化】Playwright基础教程(七)Keyboard键盘
  • Java读取文件内容写入新文件
  • 学习samba
  • 【Ansible】Ansible的Ad-hoc命令执行流程
  • Postgresql 常用整理
  • 如何在Jupyter Lab中安装不同的Kernel
  • Java钩子函数的使用
  • C++跨DLL内存所有权问题探幽(一)DLL提供的全局单例模式
  • 短时间不点击云服务器,自动化断开连接,怎么设置长时间
  • typhonjs-escomplex 代码可读性 可维护度探索
  • 支持向量机基本原理,Libsvm工具箱详细介绍,基于支持向量机SVM的人脸朝向识别
  • 密码破解工具的编写
  • BES2700H开发不完全手册
  • OpenGL的学习之路-3
  • Vue 小黑记事本组件版
  • javascript如何清空数组?
  • MySQL MHA高可用切换
  • 【Python】【应用】Python应用之一行命令搭建http、ftp服务器
  • C++模拟实现——红黑树
  • java基础-数据类型
  • 设计数据库的时候会考虑哪些因素,怎样去建表?
  • AI 绘画 | Stable Diffusion精确控制ControlNet扩展插件