繁体   English   中英

Spring Boot-识别资源文件夹下的资源

[英]Spring Boot - Identify resources under resource folder

我有一个Spring Boot应用程序。 我正在尝试读取放置在main / resources文件夹下的一些文件。 我看到Spring Boot自动读取资源文件夹下的application.properties。 但是,它似乎没有读取我放置在资源文件夹下的其他xml /文本文件。

Source xslt = new StreamSource(new File("removeNs.xslt"));

我是否应该为程序添加任何其他配置,以自动读取资源文件夹下的所有文件,而无需显式提供路径?

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("removeNs.xslt").getFile());

这应该工作。

感谢@Edwin提到了一个更强大的解决方案:

File file = new File(Thread.currentThread().getContextClassLoader().getResource("removeNs.xslt").getFile());

您需要指定要使用以下命令读取资源文件夹下的特定文件

@PropertySource批注。

您可以使用以下方式指定多个属性来源

@PropertySources({
    @PropertySource(),
    @PropertySource()
})

暂无
暂无

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

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