简体   繁体   English

使用resourceLoader从Spring中的多个文件夹加载资源

[英]Loading resources from multiple folders in Spring using resourceLoader

This question is in reference with another stackoverflow post - How to load all files of a folder to a list of Resources in Spring? 这个问题是参考另一个stackoverflow文章 - 如何将文件夹的所有文件加载到Spring中的资源列表?

I want to load all files from two specific folders using ResourceLoader. 我想使用ResourceLoader加载来自两个特定文件夹的所有文件。 I am trying to use ResourcePatternUtils. 我正在尝试使用ResourcePatternUtils。

class Foobar { private ResourceLoader resourceLoader; class Foobar {private ResourceLoader resourceLoader;

@Autowired
public Foobar(ResourceLoader resourceLoader) {
    this.resourceLoader = resourceLoader;
}

Resource[] loadResources(String pattern) throws IOException {
    return ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(pattern);
}

} }

Resource[] resources1 = foobar.loadResources("classpath*:../../folder1/*.txt");
Resource[] resources2 = foobar.loadResources("classpath*:../../folder2/*.txt");

But I need both the resources in a single array. 但我需要一个阵列中的两个资源。 Should I be using something like Java8 stream to concatenate them ? 我应该使用像Java8流这样的东西来连接它们吗?

This could be possible with org.springframework.core.io.support.ResourcePatternResolver#getResources interface where you can pass path as parameter. 这可以通过org.springframework.core.io.support.ResourcePatternResolver #getResources接口实现,您可以将path作为参数传递。

resourcePatternResolver.getResources("classpath:folder/*.xml");

Implementation is on ApplicationContext so you can access from this interface also ApplicationContext上的实现也可以从这个界面访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM