DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Coding
  3. Frameworks
  4. Grails Goodness: Conditionally Load Bean Definitions from resources.groovy

Grails Goodness: Conditionally Load Bean Definitions from resources.groovy

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
Mar. 13, 13 · Interview
Like (0)
Save
Tweet
Share
7.92K Views

Join the DZone community and get the full member experience.

Join For Free

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



 

Spring Framework Grail (web browser)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Factors When Selecting a Database
  • What Java Version Are You Running? Let’s Take a Look Under the Hood of the JDK!
  • Simulate Network Latency and Packet Drop In Linux
  • The ChatGPT Model: A Real-Life Example

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: