开发者

Referencing user-defined configuration variable in Grails?

开发者 https://www.devze.com 2023-04-13 01:01 出处:网络
I\'m开发者_C百科 trying to add a section of configuration to my Config.groovy file, and reference the entries elsewhere.

I'm开发者_C百科 trying to add a section of configuration to my Config.groovy file, and reference the entries elsewhere.

This will be used for networking from different locations, and it needs route to the correct place based on who is using it.

The basic layout in the config would look something like this:

externalApp {
    host: "xxx.xxx.xx.xx"
    port: "xxxx"
}

I need to place the "host" and "port" in separate sections of the application, inside URL strings like so:

http://**externalApp.host**:**externalApp.port**

...etc. But I can't seem to find a simple way to reference the actual configuration in the file. Is there an easy way to do this?

Thanks in advance!


If you simply want to get a reference to these config parameters in your application then you can use:

def host = ConfigurationHolder.config.externalApp.host
def port = ConfigurationHolder.config.externalApp.port

def url = "http://${host}:${port}"

ConfigurationHolder is deprecated in Grails 1.3.7, you're supposed to use GrailsApplication instead. You can get a reference to an instance of this class via dependency inject, e.g.

class MyController {

  def grailsApplication
}

Then get a reference to your configuration using:

ConfigObject config = grailsApplication.config
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号