Guice:如何绑定由已绑定对象动态获取的类? [英] Guice : How to bind classes that are dynamically obtained by an already binded object?

查看:82
本文介绍了Guice:如何绑定由已绑定对象动态获取的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Guice开发一个小型Web框架.我有一个 Router 对象,该对象一旦初始化,就会公开一个 getControllerClasses()方法.我必须遍历所有那些动态返回的类,以使用Guice绑定()它们.

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.

我绑定路由器:

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

但是,如何在模块中获取绑定的Router实例,这样我也可以绑定其 getControllerClasses()方法返回的类?

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?

我能够在模块中获取Router实例的唯一方法是,通过在第一个模块中绑定此实例,然后在setter上使用@Inject将其注入第二个模块:

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 :

模块1

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

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

模块2

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

该方法被调用,并且Router实例已很好地初始化,但是控制器类的绑定失败!看来在Guice生命周期的这一步,module2的 binder 实例为NULL.

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?

推荐答案

活页夹此时为null,因为Guice在调用configure()之前设置了活页夹.在Guice调用模块的configure()方法之外,没有活页夹.请记住,Module仅仅是一个配置文件,它可以配置Injector在创建一个文件时的行为.

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的行为不像您认为的那样-也就是说,它不会立即绑定传入实例的字段.相反,您告诉它尽快注入字段和方法.有人创建Injector.在创建Injector之前,不会将任何东西注入传递的实例中.

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.

如果您的路由器没有需要Guice的依赖项,则只需创建一个new Router()并将其作为构造函数参数传递到模块中即可.然后,您可以遍历控制类并绑定它们,也可以绑定bind(Router.class).toInstance(myRouter);.

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);

然后,您的newInjector将继承您originalInjector的绑定以及路由器指定的所有控制类.

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

希望有帮助!

这篇关于Guice:如何绑定由已绑定对象动态获取的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆