11.2.1 Consuming Events 3.1.7

See the light - agile, industrial strength, rapid web application development made easy

(Quick Reference) # 11.2.1 Consuming Events - Reference Documentation Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari Version: 3.1.7 ## 11.2.1 Consuming Events There are several ways to consume an event. As mentioned previously services and controllers implement the Events trait. The `Events` trait provides several methods to register event consumers. For example:
    on("myEvent") {
        println "Event fired!"
    }
Note that if you wish a class (other than a controller or service) to be an event consumer you simply have to implement the `Events` trait and ensure the class is registered as a Spring bean. For example given the following class:

    import grails.events.*
    import javax.annotation.*class MyClass implements Events {	@PostConstruct
    	void init() {
    		on("myEvent") {
    		    println "Event fired!"
    		}
    	}
    }
You can override `doWithSpring` in your `Application` class to register it as a Spring bean (or annotate it with `Component`):
    Closure doWithSpring() {
            {->
        		myClass(MyClass)
            }
        }
| Quick Reference (hide)