google / google/guice

Support binding Module in parent modules for installation in child modules

Open
#795 1 comment 0 reactions 0 assignees View on GitHub
imported
Dominant language
Java
Stars
12.7k
Forks
1.7k
Avg merge
11m
Merged PRs (30d)
2

Description

_From [b.k.oxley](https://code.google.com/u/118426136972205251581/) on February 04, 2014 18:37:42_

"1) Binding to core guice framework type is not allowed: Module."

I wrote hm.binkley:service-binder (on Maven Central) with the goal of injecting modules for child injectors.  Essentially it a version of ServiceLoader which can inject loaded instances including non-default constructors.

They key code is:

// service - Class<T> of type to bind ala ServiceLoader.load()
// implemntations - Entries in META-INF/services for the service
// Fails when T == Module
final Multibinder<T> bindings = newSetBinder(binder, service);
for (final Class<? extends T> implementation : implementations)
    bindings.addBinding().to(implementation); // Fails here

For simple modules without dependencies I can use:

`@`Override
protected void configure() {
    for (Module module : ServiceLoader.load(Module.class))
        install(module);
}

When I use ServiceBinder to get injected modules (modules with non-default constructors):

public static final class ServiceBinderParentModule extends AbstractModule {
    `@`Override
    protected void configure() {
        ServiceBinder.with(binder()).bind(Module.class); // Fails here
     }
}

public static final class ServiceBinderChildModule extends AbstractModule {
    private final Injector guice;

    `@`Inject
    public ServiceBinderChildModule(`@`Nonnull final Injector guice) {
        this.guice = guice;
    }

    `@`Override
    protected void configure() {
        for (final Module module : guice.getInstance(Key.get(new TypeLiteral<Set<Module>>() {})))
            install(module);
    }
}

Use:

parent = createInjector(new ServiceBinderParentModule()); // Fails here
child = parent.createChildInjector(parent.getInstance(ServiceBinderChildModule.class));

I've pushed a public branch with sources and tests demonstrating the problem (tests marked `@`Ignore("Guice cannot bind Module")): https://github.com/binkley/service-binder/tree/feature/GuiceModules ENHANCEMENT REQUEST - I'd like to support this idiom.  My goal as to use DI on modules.  The dependencies go into the parent module, the child module shows up with injected modules nicely installed.

Use case is supporting modules as components.  Developers drop a jar into their project; the jar contains modules declared in META-INF/services/com.google.inject.Module; their main() includes the two lines of code directly above: it all "just works".

_Original issue: http://code.google.com/p/google-guice/issues/detail?id=795_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.