Skip to content

LifecycleListener

Randgalt edited this page Feb 21, 2013 · 6 revisions

Governator provides a mechanism to listen for injections and lifecycle state changes via the LifecycleListener interface. Add a binding to your listener in the Bootstrapping module.

You can bind multiple LifecycleListener instances. The BootstrapBinder has a method for binding LifecycleListener instances.

// to bind a LifecycleListener in the BootstrapBinder
binder.bindLifecycleListener().to(MyListener.class);

/**
 * Callback for injected instances
 */
public interface LifecycleListener
{
    /**
     * When Guice injects an object, this callback will be notified
     *
     * @param type object type being injected
     * @param obj  object being injected
     */
    public <T> void objectInjected(TypeLiteral<T> type, T obj);

    /**
     * Called when an object's lifecycle state changes
     *
     * @param obj the object
     * @param newState new state
     */
    public void     stateChanged(Object obj, LifecycleState newState);
}