java Guice:场注入的影响
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10142791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Guice: implications of field injection
提问by IAmYourFaja
My understanding of Guice is that:
我对Guice的理解是:
- Constructor-level injection (
@Inject public class Widget(Dep one, Dep two)
) impliesthat Guice will always inject that constructor every time it is invoked through anInjector
; and - Method-level injection (
@Inject public void setDepOne(Dep one)
) impliesthat Guice will always inject that method whenever it is called, so long as theWidget
object was created using the GuiceInjector
- 构造函数级注入 (
@Inject public class Widget(Dep one, Dep two)
)意味着Guice 每次通过Injector
;调用时都会注入该构造函数;和 - 方法级注入 (
@Inject public void setDepOne(Dep one)
)意味着Guice 将始终在调用该方法时注入该方法,只要该Widget
对象是使用 Guice 创建的Injector
Are these two assumptions correct? If not, please clarify!
这两个假设正确吗?如果不是,请说明!
So what I'm hung up on is: what are the implications of field-level injection?
所以我挂断的是:场级注入的含义是什么?
@Inject private Dep one;
Does this mean that Guice will always inject the property when the object is created through the Guice injector? In that case I would imagine it conflicts with constructor-level injection.
这是否意味着Guice在通过Guice注入器创建对象时会始终注入该属性?在那种情况下,我会想象它与构造函数级注入冲突。
For instance, does the following cause a conflict/error:
例如,以下是否会导致冲突/错误:
public class Widget {
@Inject private Dep one;
private Dep two;
// Dep one already injected as a field!
@Inject public Widget(Dep one, Dep two) {
// ...
}
}
Thanks in advance!
提前致谢!
回答by Ryan Nelson
Guice will always inject all fields, methods, and any single constructor annotated with @Inject
. Keep in mind the constructor always gets injected first, so your annotated field will actually overwrite that injection. Take this modified example:
Guice 将始终注入所有字段、方法和任何带有@Inject
. 请记住,构造函数总是首先被注入,因此您的注释字段实际上会覆盖该注入。以这个修改过的例子为例:
class Widget {
@Inject
private Dep one;
@Inject
public Widget(Dep one) {
this.one = one;
System.out.println(one);
}
public void printDependency() {
System.out.println(one);
}
}
class Dep {}
public class MyMain {
public static void main(String[] args) {
Injector i = Guice.createInjector();
i.getInstance(Widget.class).printDependency();
}
}
When run, this will produce something like this:
运行时,这将产生如下内容:
com.example.Dep@63238bd2
com.example.Dep@69198891
Clearly two different objects. The first line is the constructor; the second is the field injection.
显然是两个不同的对象。第一行是构造函数;二是场注入。
I have not often found a use for field injection, except to reduce verbosity when writing Guice code samples. In production code it's unwise because it makes code difficult to test.
除了在编写 Guice 代码示例时减少冗长之外,我不经常发现字段注入的用途。在生产代码中这是不明智的,因为它使代码难以测试。
回答by Louis Wasserman
Your assumptions are correct. I believe that in this particular case Guice will inject one
twice -- once through the constructor, once through the field -- if nothing else, because it can't know that they're going to the same field.
你的假设是正确的。我相信在这种特殊情况下,Guice 将注入one
两次——一次通过构造函数,一次通过字段——如果没有别的,因为它不知道它们将进入同一个字段。