在Guice注射器内部运行,@Injects不符合`new`-ed类 [英] Running inside Guice injector, @Injects not respected on `new`-ed class

查看:149
本文介绍了在Guice注射器内部运行,@Injects不符合`new`-ed类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Swing应用程序,我们希望添加一点点依赖注入来使用Guice-3.0。我们的原因是将我们的旧工厂迁移到更加灵活的位置。

We have a Swing-application we would like to add a slight touch of dependency injection to using Guice-3.0. Our reason is to migrate our old Factories into something more flexible.

我已经确保应用程序在注入器内部运行,我们现在想要一个类的方法一些字段@ Inject'ed,它出现给我未经训练的眼睛,当new运行时,该类的@Inject注释被忽略。

I have ensured that the application runs inside an injector, and we now want to have a class way down have some fields @Inject'ed, and it appears to my untrained eye that the @Inject annotations on this class is ignored when the "new" is running.

现在问题是,如果我已经正确地理解,在Guice注射器中首次运行时,新的类可以保证其@Inject注释被尊重,或者如果此属性在代码中的一个或多个新的元素之后丢失。

Question now is if I have understood correctly that classes being new'ed when first running inside a Guice injector are guaranteed to have their @Inject annotations respected, or if this property is lost after one or more new's in your code.

换句话说:

给定A从Guice获取B的实例,然后B创建新的C(),反过来运行新的D(),D恰好有@ Inject的内部,应该由Guice处理?

Given A gets an instance of B from Guice, and B then creates new C() which in turn runs new D(), and D happen to have @Inject's inside, should D be processed by Guice?

如果是,如何添加代码到我的Guice配置,让我看到新的X()是正在处理由Guice绑定,@Inject setY(Y y)正在执行?我不介意这个日志会非常大 - 我只需要验证Guice是否按照我的期望工作。

If yes, how can I add code to my Guice configuration that allows me to see that "new X()" is being processed by Guice and that "@Inject setY(Y y)" is being executed? I do not mind if this log will be very large - I just need to verify that Guice is working as I expect it to.

推荐答案

p>否,没有使用新建直接由Guice处理它不会做魔术。 Guice应该工作的方式是为您创建对象图。您不应该在课程中引用 Injector ,您不应该使用新的来创建服务

No, nothing created with new directly is handled by Guice... it doesn't do magic. The way Guice is supposed to work is that it creates your object graphs for you. You aren't supposed to reference the Injector within your classes and you aren't supposed to use new to create services that you want injected.

以下是您可能会选择的一些选项:

Here are some options you might have:


  • C 插入 B 而不是新的。将 D 注入 C

  • 如果您需要创建一个或两个这些多次在你目前的新的中,注入提供者< C> Provider< D> 而是使用 get()而不是

  • 如果在运行时只有可用的对象需要传递给这些类(某些基于用户输入或其他地方的数据)来创建它们,则可能需要使用辅助注入并注入 CFactory DFactory 而不是提供者

  • 如果你真的找不到更好的方法,可以使用 inject.injectMembers(Object)来注入对象用$ code创建新的 ...但是,如果Guice正在帮助你,那么很难说。

  • Inject C into B rather than newing it. Inject D into C.
  • If you need to create either or both of these multiple times within the class you're currently newing them in, inject Provider<C> or Provider<D> instead and use get() rather than new.
  • If there are objects only available at runtime that need to be passed to these classes (something based on user input or data from elsewhere, say) to create them, you may need to use Assisted Inject and inject a CFactory or DFactory instead of a Provider.
  • If you really can't find any better way, you can use injector.injectMembers(Object) to inject objects you've created with new... but it's hard to say if Guice is helping you much if you're doing that.

这篇关于在Guice注射器内部运行,@Injects不符合`new`-ed类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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