美文网首页
spring对资源的加载

spring对资源的加载

作者: 旋转马达 | 来源:发表于2018-11-08 11:37 被阅读0次

org.springframework.core.io.ResourceLoader是spring用于加载资源的重要机制,例如查找配置文,属性文件等资源,下面说一下怎么在我们的项目中需要的时候使用他帮助我们简化资源的访问,而不用写很繁琐的代码访问资源,还存在jar和war中不能访问的问题。

private File obtainResourceAsFile(String relativePath) {

        ResourceLoader resourceLoader = new DefaultResourceLoader();

        Resource resource = resourceLoader.getResource(relativePath);
        try {
            return resource.getFile();
        } catch (IOException e) {
            logger.error("The file cannot be obtained by resource loader, may be it's" +
                    " not exists, make sure it's exists please. "
                    + e.getLocalizedMessage());
            return null;
        }
    }

相关文章

网友评论

      本文标题:spring对资源的加载

      本文链接:https://www.haomeiwen.com/subject/woovaftx.html