Grails Domain Class Initialization

My Grails application has the following Spring bean defined in spring/resources.groovy

calendarService(CalendarService) { bean ->
    bean.initMethod = "init"     
}

      

This method looks something like this:

class CalendarService {
    void init() {
        User.findByEmail("foo@doo.com")
    }   
}

      

When I call the dynamic finder findByEmail

, I get a MissingMethodException

. My guess is that I am trying to call this method too early, that is, before the domain classes have added dynamic crawlers to their metaclass. One solution would be to call CalendarService.init()

yourself from Bootstrap.init

rather than instruct Spring to invoke it, but is there a better solution?

Thanks Don

+2


a source to share


2 answers


You are correct as described in this post , if you want dynamic methods you better go with BootStrap.groovy



BootStrap {
    def calendarService
    def init() {
        calendarService.init()
    }
}

      

+3


a source


Next steps without configuration in resources.groovy



class CalendarService {

    @PostConstruct
    private void init() {
        User.findByEmail("foo@doo.com")
    }   
}

      

0


a source







All Articles