Using Initialization bean to set properties

14 / Aug / 2012 by Sachin 0 comments

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

[java]
import org.springframework.beans.factory.InitializingBean
class MyClass implements InitializingBean {
void afterPropertiesSet() {
// do you stuff here.
}
}
[/java]

Now in resources.groovy, add

[java]
myClass(MyClass) { bean ->
bean.autowire = ‘byName’
}
[/java]

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

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *