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

发布于 2024-08-31 03:42:30 字数 380 浏览 6 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木格 2024-09-07 03:42:30

只需使用:

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

Just use:

@Value("classpath:folder/*.xml")
Resource[] resources;
忆伤 2024-09-07 03:42:30

“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);

工作得像一个魅力。

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:

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.

把时间冻结 2024-09-07 03:42:30

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 :)

你好,陌生人 2024-09-07 03:42:30

您可以尝试 corn-cps

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

在 pom.xml 中使用下面的依赖项

<dependency>
    <groupId>net.sf.corn</groupId>
    <artifactId>corn-cps</artifactId>
    <version>1.0.1</version>
</dependency>

You can try corn-cps

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

Use the dependecy below in your pom.xml

<dependency>
    <groupId>net.sf.corn</groupId>
    <artifactId>corn-cps</artifactId>
    <version>1.0.1</version>
</dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文