spring io 分析

 

spring中,配置文件是通过资源形式加载的,我们首先来分析一些在spring中资源类的结构,并且查看一下资源的类型;

资源类图如下:

 

这些实现类实际上是将java底层的二进制流包装成spring对应的resource。在这类Resource中,我们使用得最多的,要属ClassPathResourceFileSystemReource;这两种资源类分别是默认在ClassPathFileSystem下查找资源;而我们用得最多的是classPath,因为这样对工程的移植更有利。比如有个项目,项目名称为test,项目路径为:e:\test,项目的类路径为e:\test\build\classs

Resource resource = new FileSystemResource(“test.xml”);//查找的文件是e:\test\test.xml

Resource resource = new ClassPathResource(“test.xml”);// 查找的文件是e:\test\build\classs\test.xml

当使用UrlResource时,如果传递的参数是使用的file协议,则从FileSystem下查找资源。

public class FileSystemResource extends AbstractResource {

 

    private final File file;

 

    private final String path;

 

    public FileSystemResource(File file) {

        Assert.notNull(file, "File must not be null");

        this.file = file;

        this.path = StringUtils.cleanPath(file.getPath());

    }

 

  

    public FileSystemResource(String path) {

        Assert.notNull(path, "Path must not be null");

        this.file = new File(path);

        this.path = StringUtils.cleanPath(path);

    }

 

   

    public final String getPath() {

        return this.path;

    }

 

 

    public boolean exists() {

        return this.file.exists();

    }

 

   

    public InputStream getInputStream() throws IOException {

        return new FileInputStream(this.file);

    }

 

    public URL getURL() throws IOException {

        return new URL(ResourceUtils.FILE_URL_PREFIX + this.file.getAbsolutePath());

    }

 

  

    public File getFile() {

        return file;

    }

 

   

    public Resource createRelative(String relativePath) {

        String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);

        return new FileSystemResource(pathToUse);

    }

 

   

    public String getFilename() {

        return this.file.getName();

    }

 

 

    public String getDescription() {

        return "file [" + this.file.getAbsolutePath() + "]";

    }

 

    public boolean equals(Object obj) {

        return (obj == this ||

            (obj instanceof FileSystemResource && this.path.equals(((FileSystemResource) obj).path)));

    }

 

    public int hashCode() {

        return this.path.hashCode();

    }

 

}

 

 

VStringUtil中,我们可以看见如下代码:

 

public static String replace(String inString, String oldPattern, String newPattern) {

        if (inString == null) {

            return null;

        }

        if (oldPattern == null || newPattern == null) {

            return inString;

        }

 

        StringBuffer sbuf = new StringBuffer();

        // output StringBuffer we'll build up

        int pos = 0; // our position in the old string

        int index = inString.indexOf(oldPattern);

        // the index of an occurrence we've found, or -1

        int patLen = oldPattern.length();

        while (index >= 0) {

            sbuf.append(inString.substring(pos, index));

            sbuf.append(newPattern);

            pos = index + patLen;

            index = inString.indexOf(oldPattern, pos);

        }

        sbuf.append(inString.substring(pos));

 

        // remember to append any characters to the right of a match

        return sbuf.toString();

    }

 

是将\\替换成\的代码,我们可见,在这段代码中,对传入的参数进行了严密的验证,正如代码大全中所介绍的防御性编程,我们假定从public传入进来的参数都是不安全的,只有在私有方法中,我们才对传入的参数不进行合法验证;

 

在这里,我们很明显的看出,代码中使用了适配器模式中的类适配器,在新的接口中,我们用resource接口包装了File

在新的接口中,我们依然能访问fileisExist方法,却丝毫不知道我们是用的file

其中,StrigUtils类是一个工具,他对path进行了预处理,处理了如//分解符和.  ..等文件路径描述的特殊操作符

另外的ByteArrayResource等资源,跟byeByteInputeStream的关系是类似的,分别是把不同源的数据当作资源,不过ClasspathResource不同的是,你可以传入ClassLoader或者Class来指定当前的类加载器,从而可以确定资源文件的BascDir

如果你不指定classLoaderClass的话,那么就会默认是当前线程的ClassLoader

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值