Accessing Grails Configuration with Spring Property Placeholders

Demonstrates using Spring property placeholders to access Grails configuration directives..

Object Partners

It seems like everybody approaches the issue of accessing their configuration from their application code in a different way. Grails gives us some helpful shortcuts that make accessing the configuration pretty easy. Simply injecting the [grailsApplication](http://grails.org/doc/latest/api/org/codehaus/groovy/grails/commons/GrailsApplication.html) bean in any of your controllers or services will let you access the magical [config](http://grails.org/doc/latest/api/org/codehaus/groovy/grails/commons/GrailsApplication.html#getConfig()) property. From there, all global and environment-specific configurations are available as attributes of the [ConfigObject](http://groovy.codehaus.org/gapi/groovy/util/ConfigObject.html) instance, and can be accessed with JavaBean-style ‘dot’ notation.

A common approach that I’ve seen in Grails applications is to create a ConfigurationService with helper methods for accessing the config. You can think of this approach as a shortcut, but it probably adds a layer of unnecessary abstraction when you want to be more explicit. It also means having another service to maintain, when you really just need a variable representing a configuration value.

Luckily, Grails is a fully loaded Spring application and the framework developers have gone to great lengths to ensure that the Grails abstraction integrates seamlessly within the Spring application context. To that end, we can realize that the Grails configuration is exposed to the Spring [Environment](http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/core/env/Environment.html) and the directives therein are able to be accessed with property placeholder expressions throughout the application.

Now, in your services and controllers you can explicitly wire properties for the configuration directive that you need by using Spring’s [@Value](http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/beans/factory/annotation/Value.html) annotation. Consider the following configuration and service that demonstrates using Spring’s constructs to assign configuration values to variables within a service.

package com.objectpartners.application.config

config {
    social {
        twitter {
            consumerKey = "ABCD"
            consumerSecret = "EFGH"
            accessToken = "IJKL"
            accessTokenSecret = "MNOP"
        }
        faceboook {
            ...
        }
        linkedin {
            ...
        }
    }
}

It’s important to note the single quotes in the annotation value field. This is necessary due to Groovy and the Spring Expression Language (SpEL) sharing the same notation for evaluating expressions. In this case, we don’t want Groovy to try to interpret this as an expression, as it will only make sense to the SpEL parser.

Share this Post

Related Blog Posts

JVM

Poly Driver: A Phantom Band-Aid for Geb

May 6th, 2013

An introduction to the Poly Driver plugin.

Object Partners
JVM

HTML-Encoding UTF-8 Characters

April 24th, 2013

It happens sometimes that a web page isnt using UTF-8, but theres a need to display UTF-8 data. Thankfully HTML offers encoding that allows displaying any arbitrary UTF-8 characters.

Object Partners
JVM

Refactoring? Check your settings!

April 16th, 2013

When refactoring, be sure to check your application settings as they may no longer make sense.

Brendon Anderson

About the author