Eclipse @Singleton创建了两次

时间:2016-01-10 02:31:35

标签: eclipse-rcp

我有一个持久化的bean,

@Creatable
@Singleton
public class PersistentBean extends PersistentType {

  private final Helper helper;
  private final IEventBroker eventBroker;

  @Inject
  public PersistentBean(Helper helper, IEventBroker eventBroker) {
    this.helper = helper;
    this.eventBroker = eventBroker;
  }

  @PostConstruct
  private void postConstruct() {
    setTaxYear(TaxYear.TY2013.toString());
  }

  // stuff ...
}

激活Eclipse跟踪后,此ctor和postConstruct()称为TWICE。奇怪的是这个班级是@Singleton?

setTaxYear()触发解组并要求Helper计算各种结果。

当Helper调用Provider.get()时,它会收到null并崩溃。那是什么?已经创建的单例bean正在调用它。

提前感谢任何建议,我读了相关的帖子没有成功, 戴夫。

--- Greg的更新

public class PersistentBean extends PersistentType {

  public void setTaxYear(String taxYear) {
    unmarshal(taxYear);
  }

  private void unmarshal(String taxYear) {
    // Unmarshal file into jaxBean, works fine.
    PersistentType jaxBean = <elided>;
    // Copy jaxBean into this object's state, no problem.
    // Now update derived data (mucho problema).
    helper.updateTax();
  }
}

public class Helper extends AClass {

  private final Provider<PersistentBean> persistentProvider;
  private PersistentBean persistent;

  @Inject
  public Helper(Provider<PersistentBean> persistentProvider) {
    this.persistentProvider = persistentProvider;
  }

  BigDecimal updateTax() {
    if (persistent == null)
      persistent = persistentProvider.get();
    // Result is null, attempts to use cause NPE.
    // And sometimes (?) goes into infinite loop?
  }
}

令人困惑。我能想到的是@PostConstruct仍然在PersistentBean构造函数的上下文中运行,所以当Helper试图获取它时,PersistentBean没有正式创建?也许E4在构造函数和postConstructor返回之前都不知道@Singleton?使初始化状态变得有点困难。

0 个答案:

没有答案