Spring源码学习-2.IOC-ApplicationContext资源定位

resource定位过程即容器寻找数据的过程,

IOC初始化有三个步骤要完成

1,Resource定位

2,BeanDefinition加载

3,BeanDefinition注册

我们使用FileSystemXmlApplicationContext类来加载XML配置文件,进而分析IOC容器是如何完成初始化的

<pre name="code" class="java">//  追踪父类。AbstractApplicationContext  获取加载器

public abstract class AbstractApplicationContext extends DefaultResourceLoader  
<span style="white-space:pre">	</span>implements ConfigurableApplicationContext, DisposableBean {  
//静态初始化块,在整个容器创建过程中只执行一次  
static {  
<span style="white-space:pre">	</span>//为了避免应用程序在Weblogic8.1关闭时出现类加载异常加载问题,加载IoC容  
//器关闭事件(ContextClosedEvent)类  
<span style="white-space:pre">	</span>ContextClosedEvent.class.getName();  
}  
//FileSystemXmlApplicationContext调用父类构造方法调用的就是该方法  
public AbstractApplicationContext(ApplicationContext parent) {  
<span style="white-space:pre">	</span>this.parent = parent;  
<span style="white-space:pre">	</span>this.resourcePatternResolver = getResourcePatternResolver();  
}  
//获取一个Spring Source的加载器用于读入Spring Bean定义资源文件 


protected ResourcePatternResolver getResourcePatternResolver() {  
<span style="white-space:pre">	</span>// AbstractApplicationContext继承DefaultResourceLoader,也是一个S  
<span style="white-space:pre">	</span>//Spring资源加载器,其getResource(String location)方法用于载入资源  
<span style="white-space:pre">	</span>return new PathMatchingResourcePatternResolver(this);  
}   
……  
}  


 
	//  实现创建资源加载器  因为所有的application体系都是继承DefaultResourceLoader
	//所以上面的PathMatchingResourcePatternResolver(this),即可创建加载器

	public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {  
	Assert.notNull(resourceLoader, "ResourceLoader must not be null");  
	//设置Spring的资源加载器  
	this.resourceLoader = resourceLoader;  
	}


	//   (核心代码)	AbstractRefreshableConfigApplicationContext定位Bean定义资源文件:
	//   	configLocations 
	//String CONFIG_LOCATION_DELIMITERS = ",; /t/n";  
	//一个字符串可以存储一个 。或多个资源文件路径之间用” ,; /t/n”分隔,解析成数组形式
	public void setConfigLocations(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;
	}
	}		


IOC资源定位的过程比较简单,.只需要将每个资源文件都赋值给configLocations数组就可以

今天累了,明天继续整理加载部分







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值