Spring中的ClassPathXmlApplicationContext源码详解

 更新时间:2023年12月01日 10:01:38   作者:军伟@  
这篇文章主要介绍了Spring中的ClassPathXmlApplicationContext源码详解,ApplicationContext的主要实现类是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,前者默认从类路径加载配置文件,后者默认从文件系统中装载配置文件,需要的朋友可以参考下

ClassPathXmlApplicationContext源码

ApplicationContext应用上下文体系如下:

在实现类ClassPathXmlApplicationContext中其实并没有多少重要的操作,主要是在构造函数中配置Spring配置文件的路径:

public class DaoOperationMain {
	public static void main(String[] args) {
		ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
}

具体的applicationContext.xml解析相关的操作都在父类refresh中进行操作。

ClassPathXmlApplicationContext的源码如下:

/**
 * 并没有太多具体的操作,主要是初始化构造函数,主要的操作都在父类中
 */
public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext {
	private Resource[] configResources;
	public ClassPathXmlApplicationContext() {
	}
	public ClassPathXmlApplicationContext(ApplicationContext parent) {
		super(parent);
	}
	public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
		this(new String[] {configLocation}, true, null);
	}
	public ClassPathXmlApplicationContext(String... configLocations) throws BeansException {
		this(configLocations, true, null);
	}
	public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {
		this(configLocations, true, parent);
	}
	public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {
		this(configLocations, refresh, null);
	}
	public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
			throws BeansException {
		super(parent);
		//设置spring的配置文件
		setConfigLocations(configLocations);
		if (refresh) {
			//调用父类的refresh函数,进行一系列初始化
			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, null);
	}
	public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz, 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);
		}
		//调用父类的refresh函数,进行一系列初始化
		refresh();
	}
	@Override
	protected Resource[] getConfigResources() {
		return this.configResources;
	}
}

到此这篇关于Spring中的ClassPathXmlApplicationContext源码详解的文章就介绍到这了,更多相关ClassPathXmlApplicationContext源码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java 读取本地文件实例详解

    java 读取本地文件实例详解

    这篇文章主要介绍了java 读取本地文件实例详解的相关资料,需要的朋友可以参考下
    2017-05-05
  • Java中的logback标记日志过滤器MarkerFilter详解

    Java中的logback标记日志过滤器MarkerFilter详解

    这篇文章主要介绍了Java中的logback标记日志过滤器MarkerFilter详解,在logback-classic中存在一个全局过滤器TurboFilter,TurboFilter是与LoggerContext绑定,会在会在其它过滤器之前执行,需要的朋友可以参考下
    2023-11-11
  • Java中==与equals的区别小结

    Java中==与equals的区别小结

    这篇文章主要介绍了Java中==与equals的区别小结,本文总结结论:== 与 equals()比较的内容是不同的,equals()方式是String类中的方法,它用于比较两个对象引用所指的内容是否相等,而 == 比较的是两个对象引用的地址是否相等,需要的朋友可以参考下
    2015-06-06
  • Java使用锁解决银行取钱问题实例分析

    Java使用锁解决银行取钱问题实例分析

    这篇文章主要介绍了Java使用锁解决银行取钱问题,结合实例形式分析了java线程同步与锁机制相关原理及操作注意事项,需要的朋友可以参考下
    2019-08-08
  • 在IntelliJ IDEA中多线程并发代码的调试方法详解

    在IntelliJ IDEA中多线程并发代码的调试方法详解

    这篇文章主要介绍了在IntelliJ IDEA中多线程并发代码的调试方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • JVM的常用命令汇总

    JVM的常用命令汇总

    监测java应用,最方便的就是直接使用jdk提供的现成工具,在jdk的安装的bin目录下,已经提供了多种命令行监测工具。本文为大家总结了几个JVM的常用命令,需要的可以参考一下
    2022-10-10
  • SpringBoot集成nacos动态刷新数据源的实现示例

    SpringBoot集成nacos动态刷新数据源的实现示例

    这篇文章主要介绍了SpringBoot集成nacos动态刷新数据源的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Spring Cloud Gateway详细使用最佳实践

    Spring Cloud Gateway详细使用最佳实践

    Spring Cloud Gateway 是 Spring Cloud 生态系统中的现代化 API 网关组件,用于构建微服务架构中的统一入口网关,本文介绍Spring Cloud Gateway详细使用最佳实践,感兴趣的朋友一起看看吧
    2025-11-11
  • spring的jdbctemplate的crud的基类dao

    spring的jdbctemplate的crud的基类dao

    本文主要介绍了使用spring的jdbctemplate进行增删改查的基类Dao的简单写法,需要的朋友可以参考下
    2014-02-02
  • springboot使用redisTemplate操作lua脚本

    springboot使用redisTemplate操作lua脚本

    本文主要介绍了springboot使用redisTemplate操作lua脚本,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08

最新评论