ResourceLoader的用法

public class ResourceTest {
    
    
    @Test
    public void test_DefaultResourceLoader() {
    
    

        DefaultResourceLoader resourceLoader = new DefaultResourceLoader();

        Resource unknownRes = resourceLoader.getResource("classpath:unknown.txt");
        System.out.println(unknownRes.exists()); // false

        Resource r1 = resourceLoader.getResource("classpath:file/abc.txt");
        System.out.println(r1.exists()); // true

        Resource r2 = resourceLoader.getResource("classpath:/file/abc.txt");
        System.out.println(r2.exists()); // true

        Resource r3 = resourceLoader.getResource("file:file/abc.txt");
        System.out.println(r3.exists()); // false

        Resource r4 = resourceLoader.getResource("file:/file/abc.txt");
        System.out.println(r4.exists()); // false

        Resource r5 = resourceLoader.getResource("file:d:\\remark.txt");
        System.out.println(r5.exists()); // true

        Resource r5_2 = resourceLoader.getResource("file:d:/remark.txt");
        System.out.println(r5_2.exists()); // true

        Resource r5_3 = resourceLoader.getResource("file:/d:/remark.txt");
        System.out.println(r5_3.exists()); // true

        Resource r6 = resourceLoader.getResource("file:D:\\Projects\\practice\\" +
                "demo-webmvc\\src\\main\\resources\\file\\abc.txt");
        System.out.println(r6.exists()); // true

        Resource r7 = resourceLoader.getResource("/file/abc.txt");
        System.out.println(r7.exists()); // true

        Resource r8 = resourceLoader.getResource("file/abc.txt");
        System.out.println(r8.exists()); // true


    }

    @Test
    public void test_DefaultResourceLoader2() throws IOException {
    
    
        DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource("classpath:META-INF/spring.factories");
        File file = resource.getFile(); // 抛出异常(不能加载引入的jar包下的文件)
        System.out.println(file.getName());
    }
    
    @Test
    public void test_DefaultResourceLoader2_1() throws IOException {
    
    
        DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource("classpath*:META-INF/spring.factories");
        File file = resource.getFile(); // 抛出异常(连自己创建的文件都加载不到)
        System.out.println(file.getName());
    }

    @Test
    public void test_ResourcePatternResolver3() throws IOException {
    
    
        PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource resource = resourcePatternResolver.getResource("classpath:META-INF/spring.factories");
        Properties properties = new Properties();
        properties.load(resource.getInputStream());
        System.out.println(properties); // 能正常加载到jar包下的文件(同时存在,优先加载自己创建的)
    }

    @Test
    public void test_ResourcePatternResolver3_2() throws IOException {
    
    
        PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resourcePatternResolver.getResources("classpath:META-INF/spring.factories");
        for (Resource resource : resources) {
    
    
            Properties properties = new Properties();
            properties.load(resource.getInputStream());
            System.out.println(properties); // 能正常加载到jar包下的文件,这里只加载到了1个(同时存在,优先加载自己创建的)
        }
    }

    @Test
    public void test_ResourcePatternResolver3_3() throws IOException {
    
    
        PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resourcePatternResolver.getResources("classpath*:META-INF/spring.factories");
        for (Resource resource : resources) {
    
    
            Properties properties = new Properties();
            properties.load(resource.getInputStream());
            System.out.println(properties); // 能正常加载到jar包下的文件,这里加载了2个(同时存在,优先加载自己创建的)
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_16992475/article/details/121581159