查找与模式匹配的所有 CLASSPATH 资源
我想通过使用上下文类加载器将它们加载为资源来读取一堆文本文件。
URL url = Thread.currentThread()
.getContextClassLoader()
.getResource("folder/foo.txt");
有没有办法获取名称与给定模式匹配的资源列表?例如:
URL[] matchingUrls = someLibrary.getMatchingResources("folder/*.txt");
像 Spring 这样的库可以扫描类路径来查找具有给定注释的类,所以我想知道是否有类似的东西来加载一堆资源。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需使用:
Just use:
“Binil Thomas”的评论是正确的,我正在寻找可以从 Java Config 使用 Spring 的 PathMatchingResourcePatternResolver 的确认,以便我可以将生成的“资源”列表提供给 Spring Hibernate SessionFactory.mappingLocations,而无需更新列表每次添加新映射文件时 Hibernate *.hbm.xml 文件的数量。我能够使用以下代码通过 PathMatchingResourcePatternResolver 实现此目的:
工作得像一个魅力。
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:
Works like a charm.
Spring支持ant风格的类路径资源匹配。
http://static.springsource.org/spring/docs/2.5 .x/reference/resources.html
示例:
classpath:com/mycompany/**/applicationContext.xml, /WEB-INF/*-context.xml
看看是否可以使用春天为您的项目。如果不可能,那么您可以随时拉下源代码来查看他们在做什么,然后自己做:)
Spring supports ant-style class path resource matching.
http://static.springsource.org/spring/docs/2.5.x/reference/resources.html
Examples like :
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 :)
您可以尝试 corn-cps
在 pom.xml 中使用下面的依赖项
You can try corn-cps
Use the dependecy below in your pom.xml