Spring Resource 加载文件的方式
2014-04-22 23:34
260 查看
在使用spring作为容器进行项目开发中会有很多的配置文件,这些配置文件都是通过Spring的Resource接口来实现加载,其实Spring的Resource接口是对java.net.URL的一个强化,因为我们的配置文件不仅仅是来自于http请求或者ftp请求,还有很多classpath或者fileSystem或者InputStream(很少见),为了解决这个需求Spring开发了Resource接口:
Resource接口源码如下:
Resource只是一个标准,一个用于解决上面说到的需求的标准,具体的使用方式有以下几种:
1.引用http资源:
2.引用classpath资源:
3.引用fileSystem资源:
4.使用相对路径的资源:
code如下:
Resource接口源码如下:
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只是一个标准,一个用于解决上面说到的需求的标准,具体的使用方式有以下几种:
1.引用http资源:
2.引用classpath资源:
3.引用fileSystem资源:
4.使用相对路径的资源:
code如下:
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");
相关文章推荐
- 加载指定文件的方式,使用getResourceAsStream("fileName")
- Spring加载xml配置文件的方式 ApplicationContext
- properties资源文件加载方式
- Spark生态之Alluxio学习14--alluxio内存文件加载方式和分布情况分析
- 动态加载plist文件和SDK接口使用方式
- MFC中怎样将位图写入指定的Picture控件中,两种方式:1 加载已有位图 2读取位图文件;另外CreateDIBitmap的用法和StretchDIBits用法举例
- 加载.properties文件的几种方式
- spring加载hibernate映射文件的几种方式
- php两种include加载文件方式效率比较
- 工程目录中文件加载方式
- Android加载SD卡目录,文件夹遍历,图片设置,设置文件对应打开方式等
- java 加载文件的几种方式
- velocity中加载模板文件的方式
- 这种方式加载配置文件是一种好的方式
- java加载文件的3种方式
- SpringMVC加载配置Properties文件的几种方式
- spring配置文件的概念和加载方式?
- Spring学习-03:ApplicationContext加载配置文件的两种方式
- JAVA ResourceBundle 加载外部路径资源文件方式
- 动态加载JavaScript文件的3种方式