Grails - Cannot read externalized configuration values

て烟熏妆下的殇ゞ 提交于 2019-12-11 20:00:39

问题


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.


回答1:


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




回答2:


Your problem is in the order of the parameters. All the "-D" params are java parameters so, if you check the java command that is running background does not recognize those params at the end. In short, what you should do is:

grails -Dmy.property="secret" prod run-app 


来源:https://stackoverflow.com/questions/24276099/grails-cannot-read-externalized-configuration-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!