October 7, 2013

Grails Goodness: Setting Property Values through Configuration

In Grails we can define properties for services, controller, taglibs, Spring components and other components in the Spring application context through configuration. This is called property overriding in Spring terminology. This feature is very useful to define or override property values on components in the application context. We can define the property values in a beans{} block in Config.groovy. We can also load external configuration files, like property files and define property values in those.

With property overriding we don't have to look for property values via an injected GrailsApplication object and the config property of GrailsApplication. The code using the property value is now much cleaner and easier to test.

In the following sample configuration we override a message property value for the SampleService, the controller SampleController, tag library SampleTagLib and a Spring component MessageProvider:

// File: grails-app/conf/Config.groovy
import com.mrhaki.grails.conf.*

import com.mrhaki.grails.conf.SampleTagLib as STL

beans {
    // Grails service.
    sampleService {
        message = 'Value for com.mrhaki.grails.conf.SampleService'

    // A controller class is defined as Spring bean with
    // the name of the class including the package.
    "${SampleController.canonicalName}" {
        message = 'Value for com.mrhaki.grails.conf.SampleController'

    // A tag library class is defined as Spring component where
    // the name is the class name including the package.
    "${STL.canonicalName}" {
        message = 'Value for com.mrhaki.grails.conf.SampleTagLib'

    // Spring bean. Can be configured for example in 
    // grails-app/conf/resources.groovy as:
    // beans = { 
    //     messageProvider(com.mrhaki.grails.conf.MessageProvider)
    // }
    messageProvider {
        message = 'Value for com.mrhaki.grails.conf.MessageProvider'

The information can also be included in an externalized configuration file. In the following Config.groovy we load the the file beans-config.properties to set the value of the message property.

// File: grails-app/conf/Config.groovy
grails.config.locations = ['classpath:beans-config.properties']

The property file has the following content:

# File: src/java/beans-config.properties
beans.sampleService.message = Value for com.mrhaki.grails.conf.SampleService
beans.com.mrhaki.grails.conf.SampleController.message = Value for com.mrhaki.grails.conf.SampleController
beans.com.mrhaki.grails.conf.SampleTagLib.message = Value for com.mrhaki.grails.conf.SampleTagLib
beans.messageProvider.message = Value for com.mrhaki.grails.conf.MessageProvider

The following code sample shows the implementation of the Grails service SampleService to see how we define the property and use the value defined in the configuration:

// File: grails-app/service/com/mrhaki/grails/conf/SampleService.groovy
package com.mrhaki.grails.conf

class SampleService {

    String message

    String tell() {
        "I want to tell you $message"

Code written with Grails 2.2.4