imgn inject API GitHub

Construction listeners

The problem

A framework built on the container often needs to act on every instance of some kind as soon as it exists: register each window-scoped bean implementing a veto interface, attach generated commands to the view they belong to. Asking every bean to register itself spreads that duty across user code, and scanning the graph after the fact misses instances created later.

How it solves it

A ConstructionListener contributed with @IntoSet is called with every instance the container builds, and the injector it belongs to:

@Singleton
@IntoSet(ConstructionListener.class)
public final class VetoRegistrar implements ConstructionListener {
    @Inject
    public VetoRegistrar() {}

    @Override
    public void constructed(Object instance, Injector injector) {
        if (instance instanceof ExitVeto veto) {
            injector.getInstance(ExitVetoes.class).add(veto);
        }
    }
}

The root injector resolves every listener when it is created. From then on, the generated factories call each listener synchronously, on the constructing thread, once an instance is fully built: after its constructor, member injection and @PostConstruct. That covers singletons, unscoped beans, child-local beans, @Provides results, interception proxies and assisted products.

Eager singletons are built right after the listeners are resolved, so the listeners see them and their dependencies.

The injector passed along is the one the instance belongs to: the root for a singleton, the child that built an unscoped bean, and for a child-local bean or a scoped assisted product, the child that hosts it.

Limitations