查找与模式匹配的所有CLASSPATH资源

我想通过使用上下文类加载器将它们作为资源加载来读取一堆文本文件。

URL url = Thread.currentThread() .getContextClassLoader() .getResource("folder/foo.txt"); 

有没有办法获得名称与给定模式匹配的资源列表? 例如:

 URL[] matchingUrls = someLibrary.getMatchingResources("folder/*.txt"); 

像Spring这样的库可以扫描类路径以查找具有给定注释的类,所以我想知道是否有类似的东西加载一堆资源。

Spring支持ant-style类路径资源匹配。

http://static.springsource.org/spring/docs/2.5.x/reference/resources.html

示例如: classpath:com/mycompany/**/applicationContext.xml, /WEB-INF/*-context.xml

看看你是否可以在项目中使用spring。 如果不可能那么你可以随时下载源代码来看看他们在做什么,并自己做:)

只需使用:

 @Value("classpath:folder/*.xml") Resource[] resources; 

来自“Binil Thomas”的评论在正确的轨道上,我正在寻找确认可以从Java Config使用Spring的PathMatchingResourcePatternResolver,以便我可以将生成的“资源”列表提供给Spring Hibernate SessionFactory.mappingLocations而无需更新列表每次添加新的映射文件时都会生成Hibernate * .hbm.xml文件。 我能够使用以下代码使用PathMatchingResourcePatternResolver实现此目的:

 import org.hibernate.SessionFactory; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.orm.hibernate4.LocalSessionFactoryBean; ... ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver(); Resource [] mappingLocations = patternResolver.getResources("classpath*:mappings/**/*.hbm.xml"); sessionFactory.setMappingLocations(mappingLocations); 

奇迹般有效。

你可以试试corn-cps

  List resources = CPScanner.scanResources(new PackageNameFilter("net.sf.corn.cps.*"), new ResourceNameFilter("*.xml")); 

在pom.xml中使用下面的dependecy

  net.sf.corn corn-cps 1.0.1