Accessing Grails Config from a Grails Service

Grails configuration, particularly environment specific, information is kept in grails-app/conf/Config.groovy.

Access to this information is very easy in the presentation layer (controllers, gsp) using:

def imagesDir = grailsApplication.config.uploadDir

You can use this same mechanism from within a service; you just have to inject the grailsApplication into the service

class NewsService {
     def grailsApplication
     ...
     def imagesDir = grailsApplication.config.uploadDir

If you get an error message similar to the following, then the grailsApplication has  not been injected:

groovy.lang.MissingPropertyException: No such property: grailsApplication for class: NewsService

Leave a comment