title: Custom event listener example in Grails documentation
body: <p>I'm trying to add a custom GORM event listener class in <code>Bootstrap.groovy</code>, as described in the <a href="http://grails.org/doc/latest/guide/single.html#eventsAutoTimestamping" rel="nofollow">Grails documentation</a> but its not working for me. Here is the code straight from the docs:</p>
  
  <pre><code>def init = {
      application.mainContext.eventTriggeringInterceptor.datastores.each { k, datastore -&gt;
          applicationContext.addApplicationListener new MyPersistenceListener(datastore)
      }
  }
  </code></pre>
  
  <p>When I run it, the compiler complains that application and applicationContext are null. I've tried adding them as class level members but they don't get magically wired up service-style. The closest I've got so far is:</p>
  
  <pre><code>def grailsApplication
  def init = { servletContext -&gt;
      def applicationContext = servletContext.getAttribute(ApplicationAttributes.APPLICATION_CONTEXT)
      grailsApplication.mainContext.eventTriggeringInterceptor.datastores.each { k, datastore -&gt;
          applicationContext.addApplicationListener new GormEventListener(datastore)
      }
  }
  </code></pre>
  
  <p>But I still get errors: <code>java.lang.NullPointerException: Cannot get property 'datastores' on null object</code>.</p>
  
  <p>Thanks for reading...</p>
  
  <p>EDIT: version 2.2.1</p>