Dependency injection e4依赖项回注顺序:字段与方法
我很惊讶地看到,对象重新注入的顺序没有确定性行为Dependency injection e4依赖项回注顺序:字段与方法,dependency-injection,e4,Dependency Injection,E4,我很惊讶地看到,对象重新注入的顺序没有确定性行为 public class Test { @Inject private Boolean testBool; @Inject public void checkNewObject(Boolean testBoolNew) { if (!testBoolNew.equals(this.testBool)) { System.out.println("Out of sync!");
public class Test {
@Inject private Boolean testBool;
@Inject
public void checkNewObject(Boolean testBoolNew) {
if (!testBoolNew.equals(this.testBool)) {
System.out.println("Out of sync!");
} else {
System.out.println("In sync!");
}
}
}
这就是我如何使用这个类:
context.set(Boolean.class, new Boolean(true));
Test test = ContextInjectionFactory.make(Test.class, context);
context.set(Boolean.class, new Boolean(false));
因此,有时我会得到输出:
In sync!
In sync!
有时我会:
In sync!
Out of sync!
这真的是不确定的还是我只是在监督一些事情?文档中明确指出注入顺序应该是:
@Inject
注释的公共或受保护构造函数@Inject
注释的字段中,这些字段具有令人满意的类型@Inject
注释且具有满意参数的方法中
如何在equals()
中实现MyContent
是否用MyContent
和/或@createable
注释@Singleton
如果您觉得这是一个bug,请在这里存档:告诉我们如何测试它。我只是快速地手工测试了它。如果这很重要,我可以写一些东西。你如何使用这个类是非常重要的。首先,,对
checkNewObject
方法进行依赖项注入是不可能的,因为对方法进行注入的唯一方法是使用诸如@ExecuteWhere you callingcheckNewObject
?查看EclipseContext
源代码,我认为这是因为要重新注入的值位于HashMap
因此它们重新注入的顺序取决于使顺序不可预测的哈希代码。没错,第一次注入遵循顺序:字段,然后是方法。我编辑了显示这种行为的测试。未指明的顺序仅适用于回注。