Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 257
electrotype Avatar asked Oct 08 '12 00:10

electrotype


1 Answers

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!

like image 130
Jeff Bowman Avatar answered Oct 21 '22 06:10

Jeff Bowman