Search

Dark theme | Light theme

March 5, 2013

Grails Goodness: Conditionally Load Bean Definitions from resources.groovy

We can define new bean definitions in several ways with Grails. One of them is via the grails-app/conf/spring/resources.groovy file. This file is parsed and bean definitions are added to the Spring application context. The good thing is we can add Groovy code to this file to conditionally load bean definitions.

First we specify different bean definitions for different environments. Grails already has a development, test and production environment, but we can also use a custom environment. In the following sample we set the property of the Sample bean based on the environment the application is running in. We use the method Environment.executeForEnviroment() where we can use a DSL to indicate the name of the environment and the code we want to execute for that environment. First the code for the Sample class:

// File: src/groovy/com/mrhaki/grails/Sample.groovy
package com.mrhaki.grails

class Sample {
    String environment
}

Next we change the resources.groovy file and set the property environment to a different value for each environment:

// File: grails-app/conf/spring/resources.groovy
import com.mrhaki.grails.Sample
import grails.util.Environment

beans = {
    // Define bean with default value for property environment.
    // Bean definition is overridden in Environment.executeForCurrentEnvironment block.
    sample(Sample) {
        environment = 'default'
    }

    Environment.executeForCurrentEnvironment {
        // Override bean definition in 'development' mode.
        development {
            sample(Sample) {
                environment = 'Development'
            }
        }

        // Override bean definition for custom environment 'mrhaki'.
        // Start grails with -Dgrails.env=mrhaki to enable this environment.
        mrhaki {
            sample(Sample) {
                environment = 'Custom mrhaki'
            }
        }

    }
}

We can also use configuration properties to conditionally load bean definitions. In our resources.groovy the grailsApplication object is automatically injected. We can use this variable and get to the configuration properties. So we can define a configuration property in our grails-app/conf/Config.groovy file and use the value in resources.groovy in a condition to load a bean definition.

Let's add a new boolean property to our configuration sample.load and use it in our resources.groovy:

// File: grails-app/conf/Config.groovy
...

sample.load = true

...
// File: grails-app/conf/spring/resources.groovy
import com.mrhaki.grails.Sample

beans = {
    // Define bean with default value for property environment.
    // Bean definition is overridden in Environment.executeForCurrentEnvironment block.
    sample(Sample) {
        environment = 'default'
    }

    if (grailsApplication.config.sample.load) {
        sample(Sample) {
            environment = 'Set by configuration'
        }
    }
}

We could have achieved the same thing in this samples with changing bean properties via configuration, because in the sample we only change the property value. But we can do much more than simply change property values and for example load extra beans conditionally based on the Grails environment or a configuration property, which we cannot do with just changing properties via configuration.

Code written with Grails 2.2.1