gpt4 book ai didi

java - Guice注入(inject)空指针

转载 作者:搜寻专家 更新时间:2023-10-30 19:41:59 25 4
gpt4 key购买 nike

我们尝试用 Guice 重构一个项目。这个想法是将所有语言接口(interface)绑定(bind)到一个具体的对象,比如法语波兰语

我们有一个绑定(bind)模块:

public class StandardModule extends AbstractModule {

@Override
protected void configure() {

bind(Language.class).to(Polish.class);

}
}

还有一个使用这个注入(inject)对象的类(AboutDialog.java):

@Inject Language language;

public AboutDialog(JFrame parent) {
super(parent, "", true);
this.language=language;
this.setTitle(language.getLanguageInUse().getString("AboutDialog.title"));
this.parent = parent;
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
pack();
}

结果是:

java.lang.NullPointerException at net.sf.jmoney.gui.AboutDialog.<init>(AboutDialog.java:67)

第 67 行是:

this.setTitle(language.getLanguageInUse().getString("AboutDialog.title"));

我们的界面是:

public interface Language {

public ResourceBundle getLanguageInUse();
}

波兰语课是:

public class Polish implements Language {

private ResourceBundle languageInUse;

public Polish() {
languageInUse = ResourceBundle.getBundle(Constants.LANGUAGE_PL);
}

public ResourceBundle getLanguageInUse() {
return languageInUse;
}


}

我们迷路了......

最佳答案

您正在使用“字段注入(inject)”。这将使在构造函数中使用注入(inject)的值变得困难;即使 Guice 要创建对象(现在不会发生)或者您要使用 injector.injectMembers(aboutDialog),构造函数也会在注入(inject)器有机会注入(inject)您想要的字段之前运行.

创建一个既接受可变参数又接受注入(inject)参数的类有点棘手。这给您留下了几个选择:

  • 注入(inject) JFrame。如果您知道在创建构造函数时将使用什么 JFrame,则只需在您的模块中使用 bind(JFrame.class).toInstance(myJFrame);。然后 Guice 可以完全创建 AboutDialog。

  • 手动创建工厂。这样你就可以注入(inject) AboutDialog.Factory 并调用 create 来获取你的 AboutDialog。它看起来像这样:

    public class AboutDialog extends JDialog {

    /** Injectable factory. */
    public static class Factory {
    @Inject private Language language;

    public AboutDialog create(JFrame parent) {
    return new AboutDialog(parent, language);
    }
    }

    // no @Inject parameter; you're calling "new" yourself above!
    public AboutDialog(JFrame parent, Language language) {
    super(parent, "", true);
    this.language = language;
    // ... other initialization
    }
    }
  • 创建工厂并让 Guice 通过 assisted injection 为您连接起来.

    public class AboutDialog extends JDialog {

    public interface Factory {
    public AboutDialog create(JFrame parent);
    }

    // you need the @Inject, and also the @Assisted to tell Guice to
    // use the parameter instead of Guice bindings
    @Inject
    public AboutDialog(@Assisted JFrame parent, Language language) {
    super(parent, "", true);
    this.language = language;
    // ... other initialization
    }
    }

    public class StandardModule extends AbstractModule {
    @Override protected void configure() {
    bind(Language.class).to(Polish.class);

    // here every method in AboutDialog.Factory will be implemented
    // to create the method's return type [AboutDialog] based on
    // the parameters (like JFrame) and the bindings (like Language)
    install(new FactoryModuleBuilder().build(AboutDialog.Factory.class));
    }
    }

如问题评论中所述,确保您通过 @Injected 构造函数获取 AboutDialog(或 AboutDialog.Factory/field 或来自 Injector 本身,否则 Guice 将不知道注入(inject)参数。

关于java - Guice注入(inject)空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13298128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com