基于Xml 的IOC 容器-解析配置文件路径

XmlBeanDefinitionReader 通过调用ClassPathXmlApplicationContext 的父类DefaultResourceLoader 的getResource()方法获取要加载的资源,其源码如下

//获取Resource的具体实现方法
@Override
public Resource getResource(String location) {
	Assert.notNull(location, "Location must not be null");

	for (ProtocolResolver protocolResolver : this.protocolResolvers) {
		Resource resource = protocolResolver.resolve(location, this);
		if (resource != null) {
			return resource;
		}
	}
	//如果是类路径的方式,那需要使用ClassPathResource 来得到bean 文件的资源对象
	if (location.startsWith("/")) {
		return getResourceByPath(location);
	}
	else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
		return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
	}
	else {
		try {
			// Try to parse the location as a URL...
			// 如果是URL 方式,使用UrlResource 作为bean 文件的资源对象
			URL url = new URL(location);
			return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));
		}
		catch (MalformedURLException ex) {
			// No URL -> resolve as resource path.
			//如果既不是classpath标识,又不是URL标识的Resource定位,则调用
			//容器本身的getResourceByPath方法获取Resource
			return getResourceByPath(location);
		}
	}
}

DefaultResourceLoader 提供了getResourceByPath()方法的实现,就是为了处理既不是classpath标识,又不是URL 标识的Resource 定位这种情况。

protected Resource getResourceByPath(String path) {
	return new ClassPathContextResource(path, getClassLoader());
}

在ClassPathResource 中完成了对整个路径的解析。这样,就可以从类路径上对IOC 配置文件进行加载,当然我们可以按照这个逻辑从任何地方加载,在Spring 中我们看到它提供的各种资源抽象,比如ClassPathResource、URLResource、FileSystemResource 等来供我们使用。上面我们看到的是定位Resource 的一个过程,而这只是加载过程的一部分。例如FileSystemXmlApplication 容器就重写了getResourceByPath()方法:

@Override
protected Resource getResourceByPath(String path) {
	if (path.startsWith("/")) {
		path = path.substring(1);
	}
	//这里使用文件系统资源对象来定义bean 文件
	return new FileSystemResource(path);
}

通过子类的覆盖,巧妙地完成了将类路径变为文件路径的转换。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值