1

In grails 2.0 I could define Lists in Config.groovy and access them via grails.util.Holders.config.

What is the corresponding feature in grails 4.0.1?

user3647093
  • 363
  • 1
  • 6
  • 17

4 Answers4

2

In grails 2.0 I could define Lists in Config.groovy and access them via grails.util.Holders.config.

The best answer depends on knowing some context about from where you want to access the values but in general, you shouldn't use Holders.config (that is true in Grails 2, 3 and 4). One option you have is to let the DI container inject the List of values for you.

See the project at https://github.com/jeffbrown/user3647093configlist.

https://github.com/jeffbrown/user3647093configlist/blob/7078c5edc896afd6afb0280f126794730485c564/grails-app/conf/application.yml#L1-L7

---
music:
    drummers:
        - Neil Peart
        - Mike Portnoy
        - Bill Bruford
        - Carl Palmer

https://github.com/jeffbrown/user3647093configlist/blob/7078c5edc896afd6afb0280f126794730485c564/grails-app/init/user3647093configlist/BootStrap.groovy

package user3647093configlist

import org.springframework.beans.factory.annotation.Value

class BootStrap {

    @Value('${music.drummers}')
    List<String> drummerNames

    def init = { servletContext ->
        println 'Drummers:'
        for(String name : drummerNames) {
            println "\t$name"
        }
    }

    def destroy = {
    }
}

That outputs the following on application startup:

Drummers:
        Neil Peart
        Mike Portnoy
        Bill Bruford
        Carl Palmer

I hope that helps.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
0

Check out Config class: https://docs.grails.org/4.0.1/api/grails/config/Config.html

And according to official docs: https://docs.grails.org/latest/guide/conf.html#environments

Grails supports the concept of per environment configuration. The application.yml and application.groovy files in the grails-app/conf directory can use per-environment configuration using either YAML or the syntax provided by ConfigSlurper.

Michal_Szulc
  • 4,097
  • 6
  • 32
  • 59
  • I'm sorry, but I do not understand .yml syntax. in Config. groovy of grails 2 I simply wrote a line kommunikationsart=['Festnetz','Handy','Fax','eMail','Homepage'] and got it as a list in my domainclass by Holders.config.kommunikationsart. My simple question is now: what ist the corresponding .yml line and how to access it from my domain class? – user3647093 Jan 14 '20 at 09:14
  • https://stackoverflow.com/questions/23657086/yaml-multi-line-arrays – Michal_Szulc Jan 14 '20 at 14:03
-1

simply added 2 lines at the end of .yml

registrierungsstati: [beantragt, in Prüfung, anerkannt] 
qualitätsnachweise:[Techniker,Bachelor,Master,Diplom,Promotion] 

and retrieved them as lists in my domain Class

List getQualitätsnachweise () { 
    GrailsApplication grailsApplication
    grailsApplication.config.getProperty('qualitätsnachweise')
}

List getRegistrierungsstati () {
    GrailsApplication grailsApplication
    grailsApplication.config.getProperty('registrierungsstati')
}

see also https://objectcomputing.com/news/2016/08/31/retrieving-config-values-grails-3

user3647093
  • 363
  • 1
  • 6
  • 17
  • Thi sis a bad idea. The way the code is written, `grailsApplication.config` will result in `NullPointerException` because you have not initialized the local `GrailsApplication grailsApplication` variable. Even if you fixed that, this is not a great approach. A better idea is to let the DI container inject the config values into your class instead of your class reaching out to get the config values. – Jeff Scott Brown Jan 15 '20 at 18:15
-1

Correction:

List getQualitätsnachweise () { 
GrailsApplication grailsApplication = Holders.grailsApplication
grailsApplication.config.getProperty('qualitätsnachweise')

}

List getRegistrierungsstati () {
GrailsApplication grailsApplication = Holders.grailsApplication
grailsApplication.config.getProperty('registrierungsstati')

}

grailsApplication has to be initialized in both cases with Holders.grailsApplication, the same error is also in https://objectcomputing.com/news/2016/08/31/retrieving-config-values-grails-3

By the way it is no good Idea to use a getter for generating the List in a domain class, because hibernate comes in trouble with it, because hibernate takes the getter as a database field. Therefore I used

static List qualitaetsnachweise ()

instead of

static List getQualitaetsnachweise ()

And be careful: grailsApplication.config.getProperty('qualitaetsnachweise') does not come as a list, You'll get it as a comma separated string, that you have to convert by

... .split(',').toList()

user3647093
  • 363
  • 1
  • 6
  • 17
  • "the same error is also in https://objectcomputing.com/news/2016/08/31/retrieving-config-values-grails-3" - I am not able to generate an error using the approaches I wrote about in that article. That looks like it works as expected. – Jeff Scott Brown Jan 15 '20 at 18:16
  • `static List qualitaetsnachweise ()` - You really should not have a `static` method in a domain class to retrieve config values. There are no use cases where that is going to be the best approach. – Jeff Scott Brown Jan 15 '20 at 18:17
  • "And be careful: grailsApplication.config.getProperty('qualitaetsnachweise') does not come as a list.." - If you really want to reference `config.getProperty`, you should use the overloaded version which accepts a type as a parameter. For example, `grailsApplication.config.getProperty('qualitaetsnachweise', List, []) `. – Jeff Scott Brown Jan 15 '20 at 18:18