简体   繁体   中英

how to define and access array in grails from config.groovy file

I have a harcoded value in one of my controller

public regions = ['code1','code2']

Now have to read these values from config.groovy file,

I tried to define in config.groovy :-

region = "code1,code2"

in mycontroller :-

def aws = grailsApplication.config.awsRegions; 
public awsRegions = aws.split(",")

But it didn't work.

In the Config.groovy you can do:

awsRegions = ['Region 1', 'Region 2']

Then in your Controller you can do:

def awsRegions = grailsApplication.config.awsRegions

Your changes are not working in the comments with Sathish Kumar because you are calling your property "awsRegion" in Config.groovy and accessing it with "grailsApplicatio.config.awsRegion s ". The keys must match.

1

public regions = ['code1','code2']

should that not be

public List regions = ['code1','code2']
or 
public List<String> regions = ['code1','code2']

In the world of groovy / grails public is not required so long as it is not a static variable

2

def aws = grailsApplication.config.awsRegions; 
public awsRegions = aws.split(",")

When in doubt :

def aws = grailsApplication.config.awsRegions; 
println  "aws is ${aws} object class is ${aws.getClass()}"

You should find the println returns [element,e2,e3] within a List already. The getClass() of something tells you what it actually is so you should find it is already a list and does not require the additional split which you would do on a flat string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM