我想加载xml文件,其中包含Spring Maven项目中几个模块的一些错误定义。我想加载该文件,然后将其传递给JAXB解包程序。
这就是我到目前为止所做的
String path = "classpath*:/**/definitions/error-definition.xml";
ClassPathResource resource = new ClassPathResource(path);
unmarshaller.unmarshall(resource);
我的资源文件的位置如下
src/main/resource/module1/definitions/error-definition.xml
src/main/resource/module2/definitions/error-definition.xml
这给了我以下错误
java.io.FileNotFoundException: class path resource [classpath*:/**/definitions/error-definition.xml] cannot be resolved to URL because it does not exist
但是当我按照改变路径时
String path = "/module1/definitions/error-definition.xml";
它工作
以下是我尝试的另一张外卡,但没有运气
String paths = "classpath:/**/definitions/error-definition.xml";
String paths = "classpath*:error-definition.xml";
String paths = "classpath*:*.xml";
我想做的是使用通配符从src/main/resource
下的任何文件夹中获取xml文件
我参考了之前的几个SO答案,但仍然不明白我做错了什么。
要加载资源,请将ResourceLoader
注入到类中。您可以通过实现ResourceLoaderAware
或简单地用@Autowired
注释类型为ResourceLoader
的字段来实现这一点。
public class YourClass {
@Autowired
private ResourceLoader rl;
}
既然有了ResourceLoader
,就可以使用ResourcePatternUtils
来实际加载资源。
public Resource[] loadResources() {
return ResourcePatternUtils.getResourcePatternResolver(rl).getResources("classpath:/directory/**/*-context.xml);
}