Spring源码学习--XmlWebApplicationContext(四)

ApplicationContext应用上下文体系如下:

XmlWebApplicationContext是用于web容器的应用上下文的,其也没有太多操作,主要还是applicationContext.xml的解析操作,完整源码如下

public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext {
	/** Default config location for the root context */
	//默认配置文件地址
	public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
	/** Default prefix for building a config location for a namespace */
	public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";
	/** Default suffix for building a config location for a namespace */
	public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";
	//解析applicationContext.xml
	@Override
	protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
		// Create a new XmlBeanDefinitionReader for the given BeanFactory.
		XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
		// Configure the bean definition reader with this context's
		// resource loading environment.
		beanDefinitionReader.setEnvironment(getEnvironment());
		beanDefinitionReader.setResourceLoader(this);
		beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
		// Allow a subclass to provide custom initialization of the reader,
		// then proceed with actually loading the bean definitions.
		initBeanDefinitionReader(beanDefinitionReader);
		loadBeanDefinitions(beanDefinitionReader);
	}
	protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
	}
	protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {
		String[] configLocations = getConfigLocations();
		if (configLocations != null) {
			for (String configLocation : configLocations) {
				reader.loadBeanDefinitions(configLocation);
			}
		}
	}
	//获取默认配置文件地址
	@Override
	protected String[] getDefaultConfigLocations() {
		if (getNamespace() != null) {
			return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};
		}
		else {
			return new String[] {DEFAULT_CONFIG_LOCATION};
		}
	}
}


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架是一个开源的Java应用程序框架,用于构建企业级应用程序。它提供了一种轻量级的、非侵入式的方式来管理Java对象的生命周期和依赖关系,从而简化了应用程序的开发和维护过程。下面是对Spring框架源码的简要讲解: 1. 核心容器(Core Container):Spring框架的核心是其IoC容器(Inversion of Control)。它通过IoC容器来管理和组织应用程序中的Bean对象。在IoC容器中,Spring使用`BeanFactory`接口和其实现类`DefaultListableBeanFactory`来加载、配置和管理Bean对象。 2. 上下文(Context):Spring还提供了更高级的上下文功能,如`ApplicationContext`接口和其实现类`AnnotationConfigApplicationContext`、`XmlWebApplicationContext`等。上下文扩展了`BeanFactory`的功能,提供了更多的企业级特性,如国际化、事件传播、资源加载等。 3. AOP(Aspect-Oriented Programming):Spring框架实现了AOP的支持,用于将横切关注点(cross-cutting concerns)从核心业务逻辑中分离出来。Spring AOP基于代理模式和动态代理技术,通过面向切面编程来实现横切关注点的模块化。 4. 数据访问(Data Access):Spring框架提供了对各种数据访问技术的支持,包括JDBC、ORM(如Hibernate、MyBatis)和事务管理。它通过抽象出数据访问层的公共接口,使得应用程序可以更容易地切换和集成不同的数据访问技术。 5. Web支持:Spring框架还提供了对Web应用程序开发的支持,包括Web MVC、RESTful服务、WebSocket等。它通过`DispatcherServlet`和一系列的处理器(Handler)来处理Web请求,并提供了一套强大的机制来实现灵活的Web应用程序开发。 6. 测试支持:Spring框架还提供了对单元测试和集成测试的支持,如`JUnit`和`TestNG`的集成、模拟对象(Mock Objects)的支持等。这使得开发人员可以更方便地编写和执行各种测试用例。 需要注意的是,Spring框架是一个非常庞大而复杂的项目,其源代码具有很高的复杂性和技术难度。如果您有兴趣深入了解Spring框架的源码,建议您从官方网站下载源代码并阅读相关文档,或者参考一些专门讲解Spring源码的书籍和教程。同时,熟悉Java编程、设计模式和IoC、AOP等基本概念也是理解和阅读Spring源码的基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值