java 查找与模式匹配的所有 CLASSPATH 资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2766933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding all CLASSPATH resources matching a pattern
提问by Binil Thomas
I want to read a bunch of text files, by loading them as resources using the context classloader.
我想通过使用上下文类加载器将它们作为资源加载来读取一堆文本文件。
URL url = Thread.currentThread()
.getContextClassLoader()
.getResource("folder/foo.txt");
Is there some way to get a list of resources whose names match a given pattern? For eg:
有没有办法获得名称与给定模式匹配的资源列表?例如:
URL[] matchingUrls = someLibrary.getMatchingResources("folder/*.txt");
Libraries like Spring can scan the classpath to find classes with a given annotation, so I am wondering if there something similar to load a bunch of resources.
像 Spring 这样的库可以扫描类路径以查找具有给定注释的类,所以我想知道是否有类似的东西可以加载一堆资源。
采纳答案by Kannan Ekanath
Spring supports ant-style class path resource matching.
Spring 支持 ant 风格的类路径资源匹配。
http://static.springsource.org/spring/docs/2.5.x/reference/resources.html
http://static.springsource.org/spring/docs/2.5.x/reference/resources.html
Examples like : classpath:com/mycompany/**/applicationContext.xml, /WEB-INF/*-context.xml
例如: classpath:com/mycompany/**/applicationContext.xml, /WEB-INF/*-context.xml
See if you can use spring for your project. If it is not possible then you can always pull down the source code to see what they are doing, and do that yourself :)
看看你是否可以在你的项目中使用 spring。如果不可能,那么您可以随时下拉源代码以查看它们在做什么,然后自己做:)
回答by Igor
Just use:
只需使用:
@Value("classpath:folder/*.xml")
Resource[] resources;
回答by whitestryder
Comment from "Binil Thomas" was on the right track, I was looking for confirmation that Spring's PathMatchingResourcePatternResolver could be used from Java Config so that I could give the resulting "Resource" list to the Spring Hibernate SessionFactory.mappingLocations without having to update the list of Hibernate *.hbm.xml files every time a new mapping file was added. I was able to achieve this with the PathMatchingResourcePatternResolver using the below code:
“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);
Works like a charm.
奇迹般有效。
回答by Serhat
You can try corn-cps
你可以试试corn-cps
List<URL> resources = CPScanner.scanResources(new PackageNameFilter("net.sf.corn.cps.*"), new ResourceNameFilter("*.xml"));
Use the dependecy below in your pom.xml
在你的 pom.xml 中使用下面的依赖
<dependency>
<groupId>net.sf.corn</groupId>
<artifactId>corn-cps</artifactId>
<version>1.0.1</version>
</dependency>

