Using Initialization bean to set properties
Using Grails, we often set some application wide constants or config properties or execute certain tasks in bootstrap.groovy when the application starts . A better approach is to create a new class in src/groovy and have it implement Initialization bean interface
import org.springframework.beans.factory.InitializingBean class MyClass implements InitializingBean { void afterPropertiesSet() { // do you stuff here. } }
Now in resources.groovy, add
myClass(MyClass) { bean -> bean.autowire = 'byName' }
This would make it a spring managed bean and will be executed at the time the spring beans are initialized.
This is a no mess way of doing a task, while the application is coming up.
Thanks
Sachin Anand
sachin@intelligrape.com
@babasachinanand