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?
You can implement
InitializingBeanas 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. IfInitializingBeanisn’t sufficient you can call an initialization method fromBootStrap.groovy, e.g.and you can call the method
initializeor whatever you want in the service class. You can also do the initialization work directly inBootStrapif you don’t want that code in the service class.