[英]How to read a property from the application.conf file in play framework?
[英]Error in Loading custom conf file from application.conf
我正在使用play-2.3。 我想通過application.conf
加載我的自定義配置文件。 但是無法加載。
conf /
application.conf
mycustom.prop
我將mycustom.prop包含在application.conf中,如下所示。
include file("CAB.prop")
or
include "mycustom.prop"
or
include "mycustom"
以上都不起作用。
mycustum.prop:
myappIp=x.x.x.x
myappport=1234
在代碼中,當我嘗試訪問myAppIp時,它給了我NullPointerException
MyGlobal.java
import com.typesafe.config.ConfigFactory
...
String ip = ConfigFactory.load().getString("myAppIp"); // this line throws NullPointerException
我不確定,這是加載mycustom.prop
或獲取其值時出現的問題。
編輯:
原來,它們僅支持一些文件擴展名: https : //github.com/typesafehub/config#rationale-for-supported-file-formats 。 我測試了各種文件,但是include
只能拾取.properties
, .conf
和.json
文件。 任何其他文件類型都將被靜默忽略。
(以下不是問題,盡管Play文檔指出了這一點,但底層庫接受問題中提到的格式-出於傳統目的保留在此處)
從文檔 :
include語句由未加引號的字符串include和緊隨其后的單引號字符串組成。
所以應該
include "mycustom.prop"
您可以嘗試在mycustom.prop
包括application.conf
,然后使用-Dconfig.resource
或-Dconfig.file
啟動激活-Dconfig.file
。
您的mycustom.prop
:
include "application.conf"
# much configuration follows
並與啟動activator -Dconfig.resource=mycustom.prop
如果你的配置文件是在類路徑中(例如,在config文件夾中) -或activator -Dconfig.file=/opt/conf/mycustom.prop
如果要指定一個路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.