如何在没有环境的情况下读取 application.properties 文件 [英] How to read application.properties file without Environment

查看:38
本文介绍了如何在没有环境的情况下读取 application.properties 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请你能帮我从 Spring Boot 中的 application.properties 文件中读取属性,而不需要自动装配 Environment 并且不使用 Environment 吗?

Please can you help me to read the properties from application.properties file in Spring Boot, without autowiring the Environment and without using the Environment?

也不需要使用 ${propname}.我可以创建属性对象,但必须传递我的属性文件路径.我想从其他位置获取我的道具文件.

No need to use ${propname} either. I can create properties object but have to pass my properties file path. I want to get my prop file from another location.

推荐答案

OrangeDog 解决方案对我不起作用.它生成了 NullPointerException.

OrangeDog solution didn't work for me. It generated NullPointerException.

我找到了另一个解决方案:

I've found another solution:

ClassLoader loader = Thread.currentThread().getContextClassLoader();
Properties properties = new Properties();
try (InputStream resourceStream = loader.getResourceAsStream("application.properties")) {
    properties.load(resourceStream);
} catch (IOException e) {
    e.printStackTrace();
}

这篇关于如何在没有环境的情况下读取 application.properties 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆