Spring框架学习[Web环境中Spring的启动过程](七)

2014-11-24 03:05:48 · 作者: · 浏览: 2
bleWebApplicationContext的子类,其源码如下:

[java] view plaincopyprint public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext { //Web应用中Spring配置文件的默认位置和名称,如果没有特别指定,则Spring会根据 //此位置定义Spring Bean定义资源 public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml"; //Spring Bean定义资源默认前缀 public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/"; //Spring Bean定义资源默认后置 public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml"; //在分析Spring IoC初始化过程中我们已经分析过,加载Spring Bean定义资源的方法, //通过Spring容器刷新的refresh()方法触发 protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException { //为Spring容器创建XML Bean定义读取器,加载Spring Bean定义资源 XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); //设置Bean定义读取器,因为XmlWebApplicationContext是//DefaultResourceLoader的子类,所以使用默认资源加载器来定义Bean定义资源 beanDefinitionReader.setResourceLoader(this); //为Bean定义读取器设置SAX实体解析器 beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this)); //在加载Bean定义之前,调用子类提供的一些用户自定义初始化Bean定义读取器的方法 initBeanDefinitionReader(beanDefinitionReader); //使用Bean定义读取器加载Bean定义资源 loadBeanDefinitions(beanDefinitionReader); } //用户自定义初始化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}; } } }