Guice : How to bind classes that are dynamically obtained by an already binded object?
Asked Answered
P

1

6

I'm developing a small web framework using Guice. I have a Router object that, once initialized, expose a getControllerClasses() method. I have to loop over all those dynamically returned classes to bind() them using Guice.

I bind the Router :

bind(IRouter.class).to(Router.class);

But then, how can I obtain the binded Router instance, in a Module, so I can also bind the classes returned by its getControllerClasses() method?

The only way I've been able to get the Router instance in a Module, is by binding this instance in a first module and then injecting it in a second module, using @Inject on a setter :

Module 1

bind(IRouter.class).to(Router.class);

Module2 module2 = Module2();
requestInjection(module2);
install(module2); 

Module 2

@Inject
public void setRouter(IRouter router)
{
    Set<Class<?>> controllerClasses = router.getControllerClasses();
    for(Class<?> controllerClass : controllerClasses)
    {
        bind(controllerClass);
    }
}

The method is called and the Router instance is well initialized, but the binding of the controller classes fails! It seems the binder instance of module2 is NULL at this step of the Guice lifecycle.

How can I bind dynamically obtained classes, those classes been returned by an already binded object?

Percale answered 8/10, 2012 at 0:25 Comment(0)
C
11

Binder is null at that point because Guice sets the binder before it calls configure(). Outside of Guice calling the Module's configure() method, there is no binder. Remember that a Module is merely a configuration file, and that it configures how the Injector behaves when you create one.

requestInjection doesn't behave like you think it does--that is, it doesn't immediately bind the fields of the instance you pass in. Instead, you're telling it to inject fields and methods as soon as someone creates the Injector. Until an Injector is created, nothing will be injected into the passed instance.

If your Router doesn't have dependencies that require Guice, then just create a new Router() and pass it as a constructor parameter into your Module. Then you can loop through your controlling classes and bind them, and also bind(Router.class).toInstance(myRouter);.

However, if your Router does depend on other modules, then you can use a child Injector. First, create an Injector, get a Router instance out of it, and then pass that Router instance into another Module that binds its controlling classes. Suddenly you'll have a module (let's call it controllingClassesModule) and you can do the following:

newInjector = originalInjector.createChildInjector(controllingClassesModule);

Then, your newInjector will inherit the bindings from your originalInjector and also all of the controlling classes that the Router specifies.

Hope that helps!

Clavicorn answered 8/10, 2012 at 1:17 Comment(3)
Thank you Jeff! I actually had to use 3 injectors for some modules to have all their required dependencies injected, but now it works great...Percale
@Percale You're welcome! Hopefully you only need those first injectors as a startup step--I find it confusing to work with multiple injectors at once. If you find yourself putting Guice through too many acrobatics, and if you don't need the bound Controller classes injected further down the line, consider creating a mutable ControllerManager that you can query and populate later.Clavicorn
Why not call modules "InjectorConfiguration" instead? Or maybe GuiceObjectCreatorConfigurationIconoclast

© 2022 - 2024 — McMap. All rights reserved.