如何在Spring 3.1中使用通配符加载xml资源文件 [英] How to load xml resource file using wild card in Spring 3.1
问题描述
我想加载xml文件,其中包含Spring Maven项目中几个模块的一些错误定义。我想加载然后将文件传递给JAXB unmasheller。
I want to load xml files which contains some error definitions of several modules in a Spring Maven project. I want to load and then pass the file to a JAXB unmasheller.
这是我到目前为止所做的事情
This is what I have done so far
String path = "classpath*:/**/definitions/error-definition.xml";
ClassPathResource resource = new ClassPathResource(path);
unmarshaller.unmarshall(resource);
我的资源文件位于以下位置
My resource files are located as follows
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";
工作原理
以下是其他我尝试没有运气的外卡
Following are the other wild card which I tried with no luck
String paths = "classpath:/**/definitions/error-definition.xml";
String paths = "classpath*:error-definition.xml";
String paths = "classpath*:*.xml";
我想要做的是使用通配符从<$下的任何文件夹中获取xml文件c $ c> src / main / resource
What I want to do is to use wild card to get the xml files from any folder under src/main/resource
我之前提到过几个SO答案,但仍然无法弄清楚我做错了什么。 / p>
I referred several previous SO answers but still couldn't figure out what Im doing wrong.
推荐答案
要加载资源,请将 ResourceLoader
注入您的班级。您可以通过实现 ResourceLoaderAware
或仅使用 @注释
。 ResourceLoader
类型的字段来执行此操作自动装配Autowired
To load resource inject the ResourceLoader
into your class. You can do this by either implementing ResourceLoaderAware
or simply annotate a field of type ResourceLoader
with @Autowired
.
public class YourClass {
@Autowired
private ResourceLoader rl;
}
现在您拥有 ResourceLoader
您可以使用 ResourcePatternUtils
来实际加载资源。
Now that you have the ResourceLoader
you can use the ResourcePatternUtils
to actually load the resources.
public Resource[] loadResources() {
return ResourcePatternUtils.getResourcePatternResolver(rl).getResources("classpath:/directory/**/*-context.xml);
}
这篇关于如何在Spring 3.1中使用通配符加载xml资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!