美文网首页
Spring Resources

Spring Resources

作者: 若兮缘 | 来源:发表于2019-02-21 21:58 被阅读65次
    Resources接口

    针对于资源文件的统一接口
    UrlResource: URL对应的资源,根据一个URL地址即可构建
    ClassPathResource: 获取类路径下的资源文件
    FileSystemResource: 获取文件系统里面的资源
    ServletContextResource: ServletContext封装的资源,用于访问ServletContext环境下的资源
    InputStreamResource: 针对于输入流封装的资源
    ByteArrayResource: 针对于字节数组封装的资源

    ResourceLoader

    All application contexts implement the ResourceLoader interface,and therefore all application contexts
    may be used to obtain Resource instances.
    语义: 所有的applicationContext都实现了ResourceLoader接口,可以使用getResource方法获取资源实例
    主要方法: public Resource getResource(String arg0);
    支持的路径前缀: 如果是默认则根据applicationContext实例的创建方式

    Resource

    boolean exists(): 判断该资源是否存在
    URL getURL(): 获取资源的物理路径,资源不存在会抛出异常
    String getFilename(): 获取资源名称,即使资源不存在也能返回要查找的名称
    long contentLength(): 获取资源内容长度,资源不存在抛出异常
    示例:

    import java.io.IOException;
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.core.io.Resource;
    
    
    public class MoocResource implements ApplicationContextAware  {
        
        private ApplicationContext applicationContext;
        
        @Override
        public void setApplicationContext(ApplicationContext applicationContext)
                throws BeansException {
            this.applicationContext = applicationContext;
        }
        
        public void resource() throws IOException {
            //通过url方式返回长度为-1
            //Resource resource = applicationContext.getResource("https://spring.io/docs");
            //Resource resource = applicationContext.getResource("file:/D:/workspace/Spring/classes/config.txt");
            //Resource resource = applicationContext.getResource("classpath:config.txt");
            //因为applicationContext是通过classpath加载,所以不写前缀也一样
            Resource resource = applicationContext.getResource("config.txt");
            System.out.println(resource.getFilename());//config.txt
            if(resource.exists()){
                System.out.println(resource.getURL());//file:/D:/workspace/Spring/classes/config.txt
                System.out.println(resource.contentLength());//9
            }
            
        }
    }
    @RunWith(BlockJUnit4ClassRunner.class)
    public class TestResource extends UnitTestBase {
        
        public TestResource() {
            super("classpath:spring-resource.xml");
        }
        
        @Test
        public void testResource() {
            MoocResource resource = super.getBean("moocResource");
            try {
                resource.resource();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    <bean  id="moocResource" class="com.imooc.resource.MoocResource" ></bean>
    

    相关文章

      网友评论

          本文标题:Spring Resources

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