---
title: "Spring提供的资源访问和加载类Resource"
author: "lipiaoshui2015"
date: "2015-12-17T11:02:12Z"
tags: []
categories: ["编程语言"]
slug: "spring-resource"
draft: false
lastmod: "2015-12-18T11:12:25Z"
id: 2264530
url: "https://lipiaoshui2015.iteye.com/blog/2264530"
---

**1、Spring提供的资源访问和加载类Resource**

![](http://dl2.iteye.com/upload/attachment/0113/8199/78cf36db-5147-3ebe-a173-68e6386d8587.png)  
   
**2、applicationContext.xml中的配置信息**

![](http://dl2.iteye.com/upload/attachment/0113/8776/be870184-a77d-3cd8-8305-703ac057aa4a.png)  
 

**3、测试代码**

 

```
@Test
public void testResource() {
	// 1.通过ClassPathResource加载配置文件
	Resource resource = new ClassPathResource("applicationContext.xml");
	XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(resource);
	UserBean userBean = (UserBean) xmlBeanFactory.getBean("userBean");
	// 输出：keke
	System.out.println(userBean.getUsername());
	// 2.通过FileSystemResource加载配置文件
	Resource resource2 = new FileSystemResource(
			"D:/code/02-tanzhouedu/tz_springmvc/src/applicationContext.xml");
	XmlBeanFactory xmlBeanFactory2 = new XmlBeanFactory(resource2);
	UserBean userBean2 = (UserBean) xmlBeanFactory.getBean("userBean");
	// 输出：keke
	System.out.println(userBean2.getUsername());
}
```