Spring Resource实现对操作读取资源的增强

# Spring Resource实现对操作读取资源的增强 #

介绍

由于Spring的初始化加载通常都是要读取很多初始化参数的,通常都是从配置文件中读取。

Spring的配置文件所处环境相对复杂,Java自带读取配置文件的功能不能很好的满足Spring读取配置文件要求,因此Spring提供了Resource对其进行增强和支持。

Resource接口及实现应用

UrlResource

UrlResource封装了java.net.URL,它能够被用来访问任何通过URL可以获得的对象,例如:文件、HTTP对象、FTP对象等。

所有的URL都有个标准的String表示,这些前缀可以标识不同的URL类型,如file:访问文件系统路径,http:通过HTTP协议访问的资源,ftp:通过FTP访问的资源等

举例:
UrlResource urlResource = new UrlResource("file", "e:/testcopy.txt");

ClassPathResource

ClassPathResource这个类表示从classpath类路径获得的资源。 

它会使用线程context的类加载器(classloader)、给定的类加载器或者用来载入资源的给定类。

举例:
ClassPathResource classPathResource = new ClassPathResource("config/mytest.txt");

ServletContextResource

这是为ServletContext资源提供的Resource实现,它负责解析相关web应用根目录中的相对路径。 

备注:通常应用在java web开发中

FileSystemResource

这是为处理 java.io.File而准备的Resource实现。它既可以作为File提供,也可以作为URL。

InputStreamResource

这是为给定的InputStream而准备的Resource实现。它只有在没有其它合适的 Resource实现时才使用。

而且,只要有可能就尽量使用ByteArrayResource或者其它基于文件的Resource 实现。

ByteArrayResource

这是为给定的byte数组准备的Resource实现。它会为给定的byte数组构造一个 ByteArrayInputStream。 

它在从任何给定的byte数组读取内容时很有用,因为不用转换成单一作用的 InputStreamResource。

备注:UrlResource、ClassPathResource、ServletContextResource重点掌握

Code

/**
 * 
 * @author xuyi
 * @时间: 2016年9月12日 下午10:08:27
 * @功能:
 * @类描述:各种Resource实现应用
 * @春风十里不如你
 */
public class ResourceMain
{
    /**
     * URL资源类
     * 
     * @throws Exception
     */
    @Test
    public void testUrlResource() throws Exception
    {

        UrlResource urlResource = new UrlResource("file", "e:/testcopy.txt");

        InputStream inputStream = urlResource.getInputStream();
        FileOutputStream outputStream = new FileOutputStream("e:/testurl.txt");

        IOUtils.copy(inputStream, outputStream);

    }

    /**
     * 类路径下的资源类
     * 
     * @throws Exception
     */
    @Test
    public void testClassPathResource() throws Exception
    {

        ClassPathResource classPathResource = new ClassPathResource("config/mytest.txt");

        InputStream inputStream = classPathResource.getInputStream();
        FileOutputStream outputStream = new FileOutputStream("e:/mytest.txt");

        IOUtils.copy(inputStream, outputStream);

    }

    /**
     * 字节数组资源类
     * 
     * @throws Exception
     */
    @Test
    public void testByteArrayResource() throws Exception
    {

        ByteArrayResource byteArrayResource = new ByteArrayResource("string".getBytes());

        InputStream inputStream = byteArrayResource.getInputStream();
        FileOutputStream outputStream = new FileOutputStream("e:/hahatest.txt");

        IOUtils.copy(inputStream, outputStream);

    }

    /**
     * 系统文件资源类
     * 
     * @throws Exception
     */
    @Test
    public void testFileSystem() throws Exception
    {

        FileSystemResource fileSystemResource = new FileSystemResource("e:/test.txt");

        InputStream inputStream = fileSystemResource.getInputStream();
        FileOutputStream outputStream = new FileOutputStream("e:/testcopy.txt");

        IOUtils.copy(inputStream, outputStream);

    }
}

Java自带JDK读取配置文件

请参考我另一篇博客:

http://blog.csdn.net/nicewuranran/article/details/51598247

总结

读取特定环境下的配置文件,使用Spring的Resource不为是一种更方便的方式。

参考

1、http://docs.spring.io/spring/docs/4.2.7.RELEASE/spring-framework-reference/htmlsingle/#resources

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值