raffian
raffian

Reputation: 32056

Use variable to access config in Grails Holders

Using Grails 2.3.7, I set a property in my config file:

foo.bar = ['whatever']

I can access using Holders...

Holders.config.foo.bar    

For convenience I put Holders in util method:

static getCfgProp(key){
  Holders.config.get(key)
}

But getCfgProp('foo.bar') doesn't work (guessing because foo.bar is nested map key).

It works if I flatten the config:

static getCfgProp(key){
  Holders.getFlatConfig().get(key)
}

..but don't want to do that each time method is invoked.

Tried these, none worked, I must be missing something simple

Holders.config."${key}"
Holders.config."$key"
Holders.config.getProperty(key)
Holders.config.(key)

Upvotes: 2

Views: 1016

Answers (2)

jneira
jneira

Reputation: 944

This has worked for me with grails-2.5.6:

Holders.config[key].subkey.subsubkey...
Holders.config[key][subkey].subsubkey...

// for Holders.config.foo.bar.zet
Holders.config['foo'].bar.zet
Holders.config['foo']['bar'].zet
Holders.config['foo']['bar']['zet']

Upvotes: 0

nickdos
nickdos

Reputation: 8414

This is what I've used for displaying a config var value (via a form input):

grailsApplication.config.flatten()."${it}"

where ${it} is the input string. This works for both non-nested and nested keys due to the flatten() method.

EDIT: just realised this is the equivilent of your Holders.getFlatConfig() so probably not useful. Not sure why you

don't want to do that each time method is invoked

Performance? Have you benchmarked it?

Upvotes: 1

Related Questions