Spring中的ClassPathXmlApplicationContext源码详解
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 file.delete删除文件失败,Windows磁盘出现无法访问的文件问题
这篇文章主要介绍了Java file.delete删除文件失败,Windows磁盘出现无法访问的文件问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-06-06
SpringBoot + SpringSecurity 环境搭建的步骤
这篇文章主要介绍了SpringBoot + SpringSecurity 环境搭建的步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-05-05


最新评论