Over a million developers have joined DZone.

Grails Goodness: Setting Property Values through Configuration

DZone's Guide to

Grails Goodness: Setting Property Values through Configuration

· Java Zone
Free Resource

Learn how our document data model can map directly to how you program your app, and native database features like secondary indexes, geospatial and text search give you full access to your data. Brought to you in partnership with MongoDB.

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 ofGrailsApplication. 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

Discover when your data grows or your application performance demands increase, MongoDB Atlas allows you to scale out your deployment with an automated sharding process that ensures zero application downtime. Brought to you in partnership with MongoDB.


Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}