[英]How to load xml resource file using wild card in Spring 3.1
I want to load xml files which contains some error definitions of several modules in a Spring Maven project. 我想加载xml文件,其中包含Spring Maven项目中几个模块的一些错误定义。 I want to load and then pass the file to a JAXB unmasheller.
我想加载然后将文件传递给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
This gives me the following error 这给了我以下错误
java.io.FileNotFoundException: class path resource [classpath*:/**/definitions/error-definition.xml] cannot be resolved to URL because it does not exist
but when I change the path as follows 但是当我改变路径如下
String path = "/module1/definitions/error-definition.xml";
It works 有用
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";
What I want to do is to use wild card to get the xml files from any folder under src/main/resource
我想要做的是使用通配符从
src/main/resource
下的任何文件夹中获取xml文件
I referred several previous SO answers but still couldn't figure out what Im doing wrong. 我之前提到过几个SO答案,但仍然无法弄清楚我做错了什么。
To load resource inject the ResourceLoader
into your class. 要加载资源,请将
ResourceLoader
注入您的类。 You can do this by either implementing ResourceLoaderAware
or simply annotate a field of type ResourceLoader
with @Autowired
. 您可以通过实现
ResourceLoaderAware
或仅使用@Autowired
注释ResourceLoader
类型的字段来完成此操作。
public class YourClass {
@Autowired
private ResourceLoader rl;
}
Now that you have the ResourceLoader
you can use the ResourcePatternUtils
to actually load the resources. 现在您已拥有
ResourceLoader
您可以使用ResourcePatternUtils
来实际加载资源。
public Resource[] loadResources() {
return ResourcePatternUtils.getResourcePatternResolver(rl).getResources("classpath:/directory/**/*-context.xml);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.