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?