Finding Resources with PathMatchingResourcePatternResolver and URLClassloader in JARs

半腔热情 提交于 2019-11-28 06:47:48

Loading the files dynamically in Spring is simple, I'd change the approach to finding the files with extensions.

Try the following:

ClassLoader cl = this.getClass().getClassLoader(); 
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
Resource[] resources = resolver.getResources("classpath*:/*.xml") ;
for (Resource resource: resources){
    logger.info(resource.getFilename());
}

As Tech Trip mentioned in the comment to his answer, I had an error in my pattern. The Spring-documentation is also quiet clear about that (see Warning): "classpath*:" when combined with Ant-style patterns will only work reliably with at least one root directory before the pattern starts...originates from a limitation in the JDK's ClassLoader.getResources()

So I changed my pattern to

classpath*/model/*.myextension

Since the JARs are created from an xText-DSL I have to enforce a convention that the model-folder has to be used.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!