繁体   English   中英

如何在Spring 3.1中使用通配符加载xml资源文件

[英]How to load xml resource file using wild card in Spring 3.1

我想加载xml文件,其中包含Spring Maven项目中几个模块的一些错误定义。 我想加载然后将文件传递给JAXB unmasheller。

这就是我到目前为止所做的

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM