[英]Unable to load application.properties file
What path should I be putting in here to load this .properties
file? 我应该在此处输入什么路径来加载此.properties
文件? I can't seem to get it to load... 我似乎无法加载...
InputStream stream = ProductVersionService.class.getResourceAsStream("/application.properties");
properties.load(stream);
您需要指定从根到文件本身的属性文件的完整路径: /main/resources/application.properties
I could see, you are using Maven to build this project and hence your final jar wont have src/main/resource or src/main/java either. 我可以看到,您正在使用Maven来构建此项目,因此您的最终jar将不会具有src / main / resource或src / main / java。 All of these directories will be removed and all the contents beneath it will be added to root of jar and hence referring the path using src/main/* will result into an error. 所有这些目录将被删除,其下的所有内容都将添加到jar的根目录,因此使用src / main / *引用路径将导致错误。 You should make use of 你应该利用
Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties")
Properties prop = new Properties();
prop.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("/main/resources/application.properties"));
Since your properties
files will be read from war
package, easiest solution would be to load them as a ResourceBundle
: 由于您的properties
文件将从war
包中读取,因此最简单的解决方案是将它们作为ResourceBundle
加载:
ResourceBundle resourceBundle = ResourceBundle.getBundle("main.resources.application");
String prop = resourceBundle.getString("nameofproperty");
System.out.println(prop);
No need to develop something custom. 无需开发定制的东西。
If you follow Spring architecture you can use this class: 如果您遵循Spring体系结构,则可以使用此类:
org.springframework.core.io.support.PropertiesLoaderUtils
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.