Created
January 13, 2012 14:59
-
-
Save jdewind/1606799 to your computer and use it in GitHub Desktop.
EventBus on GuiceRoids
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ApplicationModule extends AbstractModule { | |
private final EventBus eventBus = new EventBus("Default EventBus"); | |
@Override | |
protected void configure() { | |
bind(EventBus.class).toInstance(eventBus); | |
bindListener(Matchers.any(), new TypeListener() { | |
public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) { | |
typeEncounter.register(new InjectionListener<I>() { | |
public void afterInjection(I i) { | |
eventBus.register(i); | |
} | |
}); | |
} | |
}); | |
} | |
} | |
public class ApplicationEventListener { | |
private Application application; | |
@Subscribe | |
public void applicationEvent(ApplicationEvent event) { | |
// handle event | |
} | |
} | |
public class Application { | |
private EventBus eventBus; | |
@Inject | |
public Application(EventBus eventBus) { | |
this.eventBus = eventBus; | |
} | |
public void postEvent() { | |
eventBus.post(new ApplicationEvent(this)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment