Spring Resource接口

本文介绍Spring框架中资源加载的不同方式,包括ClassPathResource、FileSystemResource和UrlResource等,并提供了一个简单的示例项目来演示如何使用这些资源加载方式。

Spring的org.springframework.core.io.Resource代表物理存在的任何资源,提供了更强的访问底层资源的功能。继承接口org.springframework.core.io.InputStreamSource。子类有ClassPathResource、FileSystemResource、UrlResource、ServletContextResource、ByteArrayResource、DescriptiveResource、InputStreamResource、PortletContextResource。常用的有:

1、ClassPathResource:以类路径的方式进行访问;

2、FileSystemResource:以文件系统的绝对路径的方式进行访问;

3、UrlResource:通过URL访问资源,也支持File格式;

4、ServletContextResource:以相对Web应用根目录的方式进行访问。

简单测试代码:

1、新建JavaWeb项目,引入jar包

spring.jar

commons-logging.jar

2、新建包leaf.spring.base.resource,在该包下新建类ResourceBean,内容如下:

package leaf.spring.base.resource;

import org.springframework.core.io.Resource;

public class ResourceBean {
	private Resource classpathResource;
	private Resource fileResource;
	private Resource urlResource;

	public Resource getClasspathResource() {
		return classpathResource;
	}

	public void setClasspathResource(Resource classpathResource) {
		this.classpathResource = classpathResource;
	}

	public Resource getFileResource() {
		return fileResource;
	}

	public void setFileResource(Resource fileResource) {
		this.fileResource = fileResource;
	}

	public Resource getUrlResource() {
		return urlResource;
	}

	public void setUrlResource(Resource urlResource) {
		this.urlResource = urlResource;
	}

}

3、在上述包路径下新建配置文件applicationContext-resource.xml,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context-3.0.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
						http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	
	<bean id="resourceBean" class="leaf.spring.base.resource.ResourceBean"> 
		<property name="classpathResource">
			<value>configs.xml</value>
		</property>
		
		<property name="fileResource">
			<value>F:\ewell\IDE\jdk-6u45-windows-i586.exe</value>
		</property>
		
		<property name="urlResource">
			<value>http://www.baidu.com</value>
		</property>
	</bean>
</beans>

4、在WEB-INF目录下新建配置文件applicationContext.xml配置文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context-3.0.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
						http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	
	<import resource="classes/leaf/spring/base/resource/applicationContext-resource.xml"/>
</beans>

5、在web.xml中配置Spring,如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

6、在步骤2的包路径下新建Test.java测试类

package leaf.spring.base.resource;

import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Test {
	public static void main(String[] args) {
		ApplicationContext ctx = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
		ResourceBean bean = (ResourceBean) ctx.getBean("resourceBean");
		
		try {
			System.out.println(bean.getClasspathResource().getFile().getPath());
			System.out.println(bean.getFileResource().getFilename());
			System.out.println(bean.getUrlResource().getURL());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

7、运行Test.java,结果如下:

configs.xml
jdk-6u45-windows-i586.exe
http://www.baidu.com

参考资料:

1、http://hi.baidu.com/shirdrn/item/5172cd089bc185f0a01034a1

2、http://blog.youkuaiyun.com/myyate/article/details/1818714

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值