[英]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.