Spring框架学习[Web环境中Spring的启动过程](三)
2014-11-24 03:05:48
·
作者:
·
浏览: 3
用户自定义初始化Bean定义读取器的方法 protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) { } //加载Bean定义资源 protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException { //获取定位的Bean定义资源路径 String[] configLocations = getConfigLocations(); if (configLocations != null) { //遍历加载所有定义的Bean定义资源 for (String configLocation : configLocations) { reader.loadBeanDefinitions(configLocation); } } } //获取默认Bean定义资源 protected String[] getDefaultConfigLocations() {//获取web.xml中的命名空间,如命名空间不为null,则返回 “/WEB-INF/命名空间.xml” if (getNamespace() != null) { return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX}; } //如果命名空间为null,则返回"/WEB-INF/applicationContext.xml" else { return new String[] {DEFAULT_CONFIG_LOCATION}; } }}
在3.(1)ContextLoader初始化Spring Web上下文的createWebApplicationContext方法创建Web应用上下文时,应用上下文对象通过setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM));方法将web.xml中配置的contextConfigLocation参数(Spring配置文件位置)设置到Spring Web应用上下文中,在XmlWebApplicationContext的loadBeanDefinitions方法加载Spring Bean定义资源文件时,会逐个加载web.xml中配置的contextConfigLocation参数的Spring Bean定义资源文件。
XmlWebApplicationContext将Web应用中配置的Spring Bean定义资源文件载入到Spring IoC容器中后,接下来的Spring IoC容器初始化和依赖注入的过程我们已经在Spring IoC容器源码分析中具体分析过,至此Web环境中Spring的工作流程分析完毕。