简体   繁体   English

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

[英]Spring Boot - Identify resources under resource folder

I have a Spring Boot App. 我有一个Spring Boot应用程序。 I am trying to read few files that I have placed under main/resources folder. 我正在尝试读取放置在main / resources文件夹下的一些文件。 I see that Spring Boot automatically reads application.properties under resources folder. 我看到Spring Boot自动读取资源文件夹下的application.properties。 However it doesn't seem to read other xml / text files that I have placed under resources folder. 但是,它似乎没有读取我放置在资源文件夹下的其他xml /文本文件。

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

Should I add any additional configuration for the program to automatically read all the files under resources folder without having to explicitly provide the path? 我是否应该为程序添加任何其他配置,以自动读取资源文件夹下的所有文件,而无需显式提供路径?

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

This should work. 这应该工作。

Thanks @Edwin for mentioning a more robust solution: 感谢@Edwin提到了一个更强大的解决方案:

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

You need to specify that you want to read specific files under resources folder using 您需要指定要使用以下命令读取资源文件夹下的特定文件

@PropertySource annotation. @PropertySource批注。

you can specify multiple property sources using 您可以使用以下方式指定多个属性来源

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

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

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