Jun 4

Using services from bootstrap.groovy in grails

Category: j2ee, java

I’m currently writing an application for some upcoming strutta.com functionality using grails.  During development though, I have it setup to recreate my data everytime I restart the server, and then I repopulate from a seperate source.

I do have a controller setup to manually import specific ids from the head end server, but its useful if I didn’t have to do this everything I hit ctrl-c in the wrong window.  Using a services orientated design, you seperate out logic from controllers (and is recommended by the grails guys), but if you need to call this from the bootup, you could follow the instructions on http://grails.org/Services (which doesn’t work…) or you could do:

import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes

class BootStrap {

  def init = { servletContext ->
    def ctx = servletContext.getAttribute(
GrailsApplicationAttributes.APPLICATION_CONTEXT);
    def ss = ctx.getBean("feedService");

    ss.loadFromFeed();
  }
  def destroy = {
  }
}

2 Comments so far

  1. Jordan June 4th, 2008 3:44 pm

    I don’t get it.

    Editor: that’s because you’re not the target market for this jordan :)

  2. Matt Passell July 17th, 2008 8:36 am

    That’s interesting that the instructions on the Grails Services page didn’t work for you. Just last night, I added a reference in Bootstrap.groovy to a service from the Acegi Security plugin and it was properly injected. Were you using Grails 1.0.3? Maybe it was something that was fixed recently…

    class BootStrap {
    AuthenticateService authenticateService

    def init = { servletContext ->

    authenticateService.passwordEncoder(…)

Leave a comment