Grails - 无法读取外部化配置值 [英] Grails - Cannot read externalized configuration values
问题描述
在开发我使用以下命令运行应用程序来运行应用程序
grails prod run-app -Dmy.property =秘密
在运行时我想使用以下代码读取控制器中的值
String myVal = System.getProperty(my.property)
当我检查myVal的值时,它始终是 null 。我只是想能够一直读取my.value的值,指针赞赏。
注意:我不想存储值在Config.groovy。我也尝试过System.getenv()。get(my.property),但这也不起作用。
我还提到了外部Grails数据源配置。
你指出的博客是正确的。以下是您可以在配置文件中添加的内容。
if(new File($ {userHome} /.$ {appName } / $ {appName} -config.groovy)。exists()){
grails.config.locations = [file:$ {userHome} /.$ {appName} / $ {appName} -config。 groovy]
println(从...加载配置:$ {grails.config.locations})
} else {
println(没有外部配置文件定义..... )
}
将它添加到你的config.groovy文件的顶部。你可以把你的外部配置文件放在下面。
假设我的appname是test
/usr/home/myhome/.test/test-config.groovy
为了在外部配置文件更改期间动态重新配置配置文件,您可以查看此插件: http:// grails.org/plugin/external-config-reload
I am developing a grails application. I want to externalize certain configuration values, so they can be specified on server startup.
In development I run the application using the following command to run the application
grails prod run-app -Dmy.property="secret"
At runtime I want to read the value in my controller using the following code
String myVal= System.getProperty("my.property")
When I check the value of myVal it is always null. I just want to be able to read the value of my.value consistently, pointers appreciated.
Note: I do not want to store values in Config.groovy. I have also tried System.getenv().get("my.property"), but that does not work either.
I have also referred to Externalizing Grails Datasource configuration.
Well, the blog which you pointed out is correct. Below is what you could add in your config file.
if (new File("${userHome}/.${appName}/${appName}-config.groovy").exists()) {
grails.config.locations = ["file:${userHome}/.${appName}/${appName}-config.groovy"]
println("loading configuration from: :${grails.config.locations}")
} else {
println("No external configuration file defined.....")
}
Add this to right at the top of your config.groovy file. The you could put your external config file as below.
Assuming my appname is "test"
/usr/home/myhome/.test/test-config.groovy
For dynamic reloading of config during changes in external config file you could look at this plugin: http://grails.org/plugin/external-config-reload
这篇关于Grails - 无法读取外部化配置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!