在使用spring做为容器进行项目开发中会有不少的配置文件,这些配置文件都是经过Spring的Resource接口来实现加载,其实Spring的Resource接口是对java.net.URL的一个强化,由于咱们的配置文件不单单是来自于http请求或者ftp请求,还有不少classpath或者fileSystem或者InputStream(不多见),为了解决这个需求Spring开发了Resource接口:java
Resource接口源码以下:spring
package org.springframework.core.io; import java.io.File; import java.io.IOException; import java.net.URI; import java.net.URL; public interface Resource extends InputStreamSource { boolean exists(); boolean isReadable(); boolean isOpen(); URL getURL() throws IOException; URI getURI() throws IOException; File getFile() throws IOException; long contentLength() throws IOException; long lastModified() throws IOException; Resource createRelative(String relativePath) throws IOException; String getFilename(); String getDescription(); }
Resource只是一个标准,一个用于解决上面说到的需求的标准,具体的使用方式有如下几种:spa
1.引用http资源:.net
2.引用classpath资源:code
3.引用fileSystem资源:blog
4.使用相对路径的资源:接口
code以下:ip
ClassPathXmlApplicationContext cx = new ClassPathXmlApplicationContext(); //http: Resource template1 = cx.getResource("http://www.chexiang.com"); //classpath: Resource template2 = cx.getResource("calsspath:xxx.text"); //fileSystem: Resource template3 = cx.getResource("file:xsd/spring-aop-3.1.xsd"); //相对路径:必须和当前的ApplicationContext路径在一块儿 Resource template4 = cx.getResource("/test/Messenger.groovy");