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!");
        } 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!

这真的是非确定性的还是我只是在监督什么?

文档明确指出注入顺序应为:

    构造函数
  1. 注入:选择使用具有最多可解析参数的@Inject注释的公共或受保护构造函数
  2. 字段注入:将值注入到带有 @Inject 注释且类型令人满意的字段中
  3. 方法注入:将值注入到带有@Inject注释且具有令人满意的参数的方法中

请参阅:https://wiki.eclipse.org/Eclipse4/RCP/Dependency_Injection#Injection_Order

我不确定,为什么这在您的情况下没有按预期工作。

  • equals()如何在MyContent中实施?
  • MyContent是否用@Creatable和/或@Singleton注释?

作为旁注:这是一个实际问题还是只是一个学术问题?为什么需要将同一实例注入到字段和同一目标实例上的方法中?如果要使用字段变量来缓存值,可以从方法中进行设置。

如果您觉得这是一个错误,请在此处提交:https://bugs.eclipse.org/bugs/enter_bug.cgi?product=Platform

最新更新