Loading...

March 26, 2014

Grails Goodness: Multiple BootStraps

In Grails we can execute code when the application starts and stops. We just have to write our code in grails-app/conf/BootStrap.groovy. Code that needs to be executed at startup must be written in the closure init. In the destroy closure we can write code that needs be executed when the application stops. But we are not limited to one BootStrap class. We can create multiple BootStrap classes as long as it is placed in the grails-app/conf directory and the name ends with BootStrap.

// File: grails-app/conf/BootStrap.groovy
class BootStrap {
    def init = { servletContext ->
        log.debug("Running init BootStrap")
    }
    def destroy = {
        log.debug("Running destroy BootStrap")
    }
}

And we can create another bootstrap class:

// File: grails-app/conf/SampleBootStrap.groovy
class SampleBootStrap {
    def init = { servletContext ->
        log.debug("Running init SampleBootStrap")
    }
    def destroy = {
        log.debug("Running destroy SampleBootStrap")
    }
}

Code written with Grails 2.3.7.