使用guice作为注入类的框架,正确的初始化方法? [英] Using guice for a framework with injected classes, proper way to initialize?

查看:655
本文介绍了使用guice作为注入类的框架,正确的初始化方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个框架,其中任意bean类都使用我的API注入类,并且它们可以与这两个类交互,并且基于定义的注释触发了回调。这是一个示例bean:

I'm trying to write a framework where arbitrary bean classes are injected with classes from my API, and they can interact with both those classes as well have triggered callbacks based on defined annotations. Here's an example bean:

@Experiment
static class TestExperiment {
    private final HITWorker worker;
    private final ExperimentLog log;
    private final ExperimentController controller;

    @Inject
    public TestExperiment(
        HITWorker worker,
        ExperimentLog expLog,
        ExperimentController controller
        ) {         
        this.worker = worker;
        this.expLog = expLog;
        this.controller = controller;
    }

    @SomeCallback
    void callMeBack() {
        ... do something

        log.print("I did something");
    }

}

我正在尝试使用Guice注入这些bean并处理注入的类之间的相互依赖性。但是,我有两个问题:

I'm trying to use Guice to inject these beans and handle the interdependencies between the injected classes. However, I have two problems:


  • 我传入的一个类( HITWorker )已经实例化。我无法看到如何将其移动到提供程序,而不会使我的代码显着复杂化。它也是持久的,但不是Guice定义的会话或请求范围,所以我现在自己管理它。 (也许如果其他问题得到克服,我可以尝试将其放在提供者中。)

  • 更重要的是,我需要引用其他注入的类,以便我可以对它们做适当的事情。当Guice注入它们时,我无法访问它们,因为bean类是任意的。

  • One of the classes I pass in (HITWorker) is already instantiated. I couldn't see how to move this to a Provider without significantly complicating my code. It is also persistent, but not to the Guice-defined session or request scope, so I am managing it myself for now. (Maybe if the other issues are overcome I can try to put this in a provider.)
  • More importantly, I need a reference to the other injected classes so I can do appropriate things to them. When Guice injects them, I can't access them because the bean class is arbitrary.

这里有一些非常糟糕的代码需要这样做,我相信这违反了所有适当的依赖注入概念。请注意, hitw 是我需要传入的唯一实例,但我也在创建其他依赖对象,因为我需要对它们的引用。使用此代码,我基本上只使用Guice作为其反射代码,而不是其依赖性解析。

Here's some really bad code for what I basically need to do, which I am sure is violating all the proper dependency injection concepts. Note that hitw is the only instance that I need to pass in, but I'm creating the other dependent objects as well because I need references to them. With this code, I'm basically only using Guice for its reflection code, not its dependency resolution.

private void initExperiment(final HITWorkerImpl hitw, final String expId) {         
    final ExperimentLogImpl log = new ExperimentLogImpl();
    final ExperimentControllerImpl cont = new ExperimentControllerImpl(log, expManager);

    // Create an experiment instance with specific binding to this HITWorker
    Injector child = injector.createChildInjector(new AbstractModule() {
        @Override
        protected void configure() {
            bind(HITWorker.class).toInstance(hitw);
            bind(ExperimentLog.class).toInstance(log);
            bind(ExperimentController.class).toInstance(cont);
        }           
    });     

    Object experimentBean = child.getInstance(expClass);        

    expManager.processExperiment(expId, experimentBean);

    // Initialize controller, which also initializes the log
    cont.initialize(expId);

    expManager.triggerStart(expId);

    tracker.newExperimentStarted(expId, hitw, cont.getStartTime());
}

我搞砸了,只需编写自己的注射码,或者在那里一种正确的方法吗?另外,我应该忘记这些bean类的构造函数注入,因为我不知道它们究竟包含什么?有没有办法获得依赖关系,如果我要求Guice注入bean而不是自己做?

Am I screwed and just have to write my own injection code, or is there a way to do this properly? Also, should I just forget about constructor injection for these bean classes, since I don't know what they contain exactly anyway? Is there any way to get the dependencies if I am asking Guice to inject the bean instead of doing it myself?

对于上下文,我一直在阅读Guice文档和看了好几天关于这个的例子,无济于事。我不认为我是一个完整的编程白痴,但我无法弄清楚如何正确地做到这一点!

For context, I've been reading the Guice docs and looking at examples for several days about this, to no avail. I don't think I'm a complete programming idiot, but I can't figure out how to do this properly!

推荐答案

你的实验似乎就像一个请求,因为它有一个明确的生命周期和一些相关的东西,实验可以随意拉入。

Your "experiment" seems to be something like a "request" in the sense that it has a defined lifecycle and some associated stuff the experiment can pull in at will.

因此,我认为您应该将所有这些内容包含在自定义范围内,如有关自定义的文档中所述作用域的。这在几个方面符合您的情况:

Therefore I think you should wrap all that into a custom scope as described in the docs about Custom Scopes. This matches your case in several points:


  • 您可以使用某些对象种子范围(您的 HITWorker

  • 生命周期:在设置实验之前执行输入范围,在完成工作后执行退出范围。

  • 访问共享内容,例如 ExperimentLog ExperimentController :绑定它们到范围。然后框架和实验实例都可以简单地 @Inject 然后获得相同的实例。

  • You can "seed" the scope with some objects (your HITWorker)
  • The lifecycle: do "enter scope" before you setup the experiment and "exit scope" after you finished your work.
  • Access to "shared" stuff like ExperimentLog and ExperimentController: Bind them to the scope. Then both the framework and the experiment instance can simple @Inject them and get the same instance.

这篇关于使用guice作为注入类的框架,正确的初始化方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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