How can I call an initialization function on a Grails service?
grails
Solution
You can implement `InitializingBean` as described by @Saurabh but that fires rather early in the Grails startup process, so while it works, the are some things that won't be available yet, for example you can't call GORM methods in domain classes because that happens after bean initialization. If `InitializingBean` isn't sufficient you can call an initialization method from `BootStrap.groovy`, e.g.
class BootStrap {
def myService
def init = { servletContext ->
myService.initialize()
}
}
and you can call the method `initialize` or whatever you want in the service class. You can also do the initialization work directly in `BootStrap` if you don't want that code in the service class.
Problem
I have a Grails service that is a wrapper around a rather complicated singleton object. I'd like to do some initializing to populate the singleton when the service is started. It would be nice if there was some kind of init() function that would be automatically called by the service when it starts, but I have found no such thing. Is there a clean way to do this?