Spring Resource接口

来源:互联网 发布:zblog php 编辑:程序博客网 时间:2024/05/04 20:12

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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://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.csdn.net/myyate/article/details/1818714

0 0
原创粉丝点击