在春季,我可以使用ResourceLoader和ResourcePatternUtils:
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[] resources = foobar.loadResources("classpath*:../../dir/*.txt");
我如何在Micronaut中做到这一点?
我找到一个解决方案
Is there an equivalent for Springs Resource in Micronaut?
加
@Inject
private DefaultClassPathResourceLoader resourceLoader;
...
Stream<URL> currencyStream = resourceLoader.getResources("currency/*.json");
long count = currencyStream.count();
...
但总是计数0 =(
最佳答案
没有与Resource
接口等效的接口,但是您可以检索URL格式的资源。
注入io.micronaut.context.env.ResourceLoader
。它有Stream<URL> getResources(String name)
编辑:一个例子
resourceLoader.getResources("some/directory/*.txt")