欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring中的ClassPathXmlApplicationContext源碼詳解

 更新時間:2023年12月01日 10:01:38   作者:軍偉@  
這篇文章主要介紹了Spring中的ClassPathXmlApplicationContext源碼詳解,ApplicationContext的主要實現(xiàn)類是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,前者默認從類路徑加載配置文件,后者默認從文件系統(tǒng)中裝載配置文件,需要的朋友可以參考下

ClassPathXmlApplicationContext源碼

ApplicationContext應用上下文體系如下:

在實現(xiàn)類ClassPathXmlApplicationContext中其實并沒有多少重要的操作,主要是在構(gòu)造函數(shù)中配置Spring配置文件的路徑:

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

具體的applicationContext.xml解析相關(guān)的操作都在父類refresh中進行操作。

ClassPathXmlApplicationContext的源碼如下:

/**
 * 并沒有太多具體的操作,主要是初始化構(gòu)造函數(shù),主要的操作都在父類中
 */
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) {
			//調(diào)用父類的refresh函數(shù),進行一系列初始化
			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);
		}
		//調(diào)用父類的refresh函數(shù),進行一系列初始化
		refresh();
	}
	@Override
	protected Resource[] getConfigResources() {
		return this.configResources;
	}
}

到此這篇關(guān)于Spring中的ClassPathXmlApplicationContext源碼詳解的文章就介紹到這了,更多相關(guān)ClassPathXmlApplicationContext源碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論