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?
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?
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.
---
music:
drummers:
- Neil Peart
- Mike Portnoy
- Bill Bruford
- Carl Palmer
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.
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
andapplication.groovy
files in thegrails-app/conf
directory can use per-environment configuration using either YAML or the syntax provided by ConfigSlurper.
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
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()