mem*_*und 12 java spring spring-batch
我有一个文件夹,想要使用Spring和通配符将所有txt文件加载到列表中:
通过注释,我可以做以下事情:
@Value("classpath*:../../dir/*.txt")
private Resource[] files;
Run Code Online (Sandbox Code Playgroud)
但是如何以编程方式使用spring实现相同的目标呢?
Mac*_*iak 25
使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
并使用它像:
Resource[] resources = foobar.loadResources("classpath*:../../dir/*.txt");
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Spring
@Autowired
private ApplicationContext applicationContext;
public void loadResources() {
try {
Resource[] resources = applicationContext.getResources("file:C:/XYZ/*_vru_*");
} catch (IOException ex) {
ex.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16477 次 |
| 最近记录: |