vir*_*hum 3 java resources spring wildcard classpath
我想加载xml文件,其中包含Spring Maven项目中几个模块的一些错误定义.我想加载然后将文件传递给JAXB unmasheller.
这就是我到目前为止所做的
String path = "classpath*:/**/definitions/error-definition.xml";
ClassPathResource resource = new ClassPathResource(path);
unmarshaller.unmarshall(resource);
Run Code Online (Sandbox Code Playgroud)
我的资源文件位于以下位置
src/main/resource/module1/definitions/error-definition.xml
src/main/resource/module2/definitions/error-definition.xml
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误
java.io.FileNotFoundException: class path resource [classpath*:/**/definitions/error-definition.xml] cannot be resolved to URL because it does not exist
Run Code Online (Sandbox Code Playgroud)
但是当我改变路径如下
String path = "/module1/definitions/error-definition.xml";
Run Code Online (Sandbox Code Playgroud)
有用
以下是我试过的另一张外卡,没有运气
String paths = "classpath:/**/definitions/error-definition.xml";
String paths = "classpath*:error-definition.xml";
String paths = "classpath*:*.xml";
Run Code Online (Sandbox Code Playgroud)
我想要做的是使用通配符从下面的任何文件夹中获取xml文件 src/main/resource
我之前提到过几个SO答案,但仍然无法弄清楚我做错了什么.
加载资源注入ResourceLoader您的类.您可以通过实施做到这一点ResourceLoaderAware或干脆标注类型的字段ResourceLoader用@Autowired.
public class YourClass {
@Autowired
private ResourceLoader rl;
}
Run Code Online (Sandbox Code Playgroud)
现在您拥有了ResourceLoader可以使用它ResourcePatternUtils来实际加载资源.
public Resource[] loadResources() {
return ResourcePatternUtils.getResourcePatternResolver(rl).getResources("classpath:/directory/**/*-context.xml);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6829 次 |
| 最近记录: |