pri_dev
pri_dev

Reputation: 11645

using properties files in groovy

I would like to externalize all my string constants in my groovy classes to a properies file, and then have them called from there. I have seen the example with the configslurper, want to know how can I make the design such that I just include/import/load the resource/property file in my class and call the property I want to on it..

something like this in my controller

include/load propertyfilename

if (propertyfilename.propertyname == mygroovyVar) {
....some code
}

i.e if possible I want to avoid using "(quotes) when retrieving properties anywhere

like the resource bundle setup for properties in spring mvc

Regards Priyank

Upvotes: 0

Views: 1090

Answers (1)

Alidad
Alidad

Reputation: 5538

You can do something like this:

something.groovy:

def loadConfig(environment,settingFileName) {
    levelConfig = new ConfigSlurper().parse(new File(settingFileName).toURI().toURL())."$environment"
}

To access it :

def configFile  = loadConfig("alpha","c:\somewhere\something.properties" 
assert configFile.currlevel = "alpha"

something.properties:

alpha  {
currlevel = "alpha"
}
beta {
currlevel = "beta"
}
prod {
currlevel = "prod"
}

If you dont want the environment tweak it a bit. Hope this can help.

Upvotes: 1

Related Questions