0

我的 spring 应用程序的类路径中有一个目录。如何使用 ResouceLoader 加载该目录中的所有文件。

// bean for test context
public class DatabaseLoader {
    @Autowired
    protected ResourceLoader myLoader;

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @PostConstruct
    public void init() throws IOException, FileNotFoundException {
        Resource[] resources = myLoader.getResource("classpath:fixtures/*.sql");
        //codepopulate in memory db with all test fixtures
        for (Resource r: resources) {
            //populate in memory db with data in this resource.
        }
    }
}
4

1 回答 1

2

PathMatchingResourcePatternResolver能够使用特殊前缀和/或内部 Ant 样式正则表达式加载资源classpath*:。例如。要加载与后缀匹配的类路径上的所有资源,请*.sql尝试以下代码段:

PathMatchingResourcePatternResolver loader = new PathMatchingResourcePatternResolver();
Resource[] resources = loader.getResources("classpath:/*.sql");
for (Resource resource : resources) {
    // process resource
}
于 2015-02-11T20:30:00.040 回答