有关如何正确连接对象的 Guice 示例

发布于 2025-01-06 05:53:22 字数 1300 浏览 0 评论 0原文

我正在尝试学习 Guice,但我有一个关于如何正确连接事物的问题。我试图在 BoofPanels doSomething() 方法中进行调用以自动使用 MockFooImpl,但我认为连接不正确。

就我而言:

@ImplementedBy(MockFooImpl.class)
public interface FooInterface {
    public int getBar(String one, String two);
}


@Singleton
public final class MockFooImpl implements FooInterface {
    @Inject
    public MockFooImpl() {
    }

    @Override
    public int getBar(String one, String two) {
      return 1;
    }
}


public class Baz extends JFrame  {

   private BoofPanel boofPanel;

   public Baz(String one, String two, Injector injector){
        // Constructor with args
        boofPanel = new BoofPanel("aString", 565);
        injector.injectMembers(boofPanel);
   }

   public static void main(String[] args){
       final Injector injector = Guice.createInjector();

       java.awt.EventQueue.invokeLater(new Runnable() {  
           @Override
           public void run() {
               final Baz baz = new Baz("one","two", injector);
               baz.setVisible(true);
           }
       });
   }
}


public class BoofPanel extends JPanel {
    @Inject
    private FooInterface fooI;

    public BoofPanel(String aString, int anInt){

    }
    public void doSomething(){
       fooI.getBar();
    }
}

任何帮助将不胜感激。

谢谢!

I am trying to learn Guice and I have a question on how to correctly wire up things. I am trying to get the call in BoofPanels doSomething() method to automatically use the MockFooImpl, but I don't think things are wired properly.

In my case I have:

@ImplementedBy(MockFooImpl.class)
public interface FooInterface {
    public int getBar(String one, String two);
}


@Singleton
public final class MockFooImpl implements FooInterface {
    @Inject
    public MockFooImpl() {
    }

    @Override
    public int getBar(String one, String two) {
      return 1;
    }
}


public class Baz extends JFrame  {

   private BoofPanel boofPanel;

   public Baz(String one, String two, Injector injector){
        // Constructor with args
        boofPanel = new BoofPanel("aString", 565);
        injector.injectMembers(boofPanel);
   }

   public static void main(String[] args){
       final Injector injector = Guice.createInjector();

       java.awt.EventQueue.invokeLater(new Runnable() {  
           @Override
           public void run() {
               final Baz baz = new Baz("one","two", injector);
               baz.setVisible(true);
           }
       });
   }
}


public class BoofPanel extends JPanel {
    @Inject
    private FooInterface fooI;

    public BoofPanel(String aString, int anInt){

    }
    public void doSomething(){
       fooI.getBar();
    }
}

Any help would be appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是偏爱你 2025-01-13 05:53:22

吉斯不是魔法。仅当它创建或您要求它时,它才会注入到对象中。如果您要创建自己的 BoofPanel,那么您需要要求 Guice 注入它。向 Baz 添加一个构造函数,如下所示:

public Baz(Injector injector) {
    boofPanel = new BoofPanel(whatever, the, params, are);
    injector.injectMembers(boofPanel);
}

BoofPanel 构造函数不需要对 fooI 变量执行任何操作;它可以将其保留为空。您还需要将 main 的注入器变量设置为最终变量,以便在 main 中创建的匿名类可以捕获它并将其传递到 Baz 构造函数中。

如果您希望由 Guice 创建 BoofPanel,那么您需要向 Guice 提供它所需的所有构造函数参数,并在 Baz 本身上调用 InjectMembers。

编辑:编辑后对上述代码的具体更改。

  1. 在此声明中添加一个final,以使其在匿​​名内部类中可用:

    final Injector Injector = Guice.createInjector();

  2. 删除此行,因为它是不必要的:

    injector.getInstance(MockFooImpl.class);

  3. 将注入器添加到 Baz 构造函数的调用中,以使其到达 BoofPanel 的创建位置(我还删除了此处不必要的 final):

    Baz baz = new Baz("one", "two", Injector);

  4. 将注入器添加到Baz 构造函数的声明,接收从主方法来看:

    public Baz(String one, String Two, Injector Injector) {

  5. 将 boofPanel 的注入添加到 Baz 构造函数中,就在 new BoofPanel 行之后,让 Guice 注入 BoofPanel:

    injector.injectMembers(boofPanel);

就是这样。

Guice isn't magic. It will only inject into an object if either it creates it, or you ask it to. If you're creating your own BoofPanel, then you'll need to ask Guice to inject it. Add a constructor to Baz that looks like:

public Baz(Injector injector) {
    boofPanel = new BoofPanel(whatever, the, params, are);
    injector.injectMembers(boofPanel);
}

The BoofPanel constructor doesn't need to do anything to the fooI variable; it can leave it null. You'll also need to make main's injector variable final, so the anonymous class created in main can capture it and pass it into the Baz constructor.

If you'd rather have BoofPanel created by Guice, then you'll need to supply all the constructor parameters it needs to Guice, and call injectMembers on Baz itself.

EDIT: Specific changes to the code above, after edits.

  1. Add a final to this declaration, to make it usable inside the anonymous inner class:

    final Injector injector = Guice.createInjector();

  2. Remove this line, as it's unnecessary:

    injector.getInstance(MockFooImpl.class);

  3. Add the injector to the invocation of the Baz constructor, to get it to where BoofPanel is created (i've also removed the unnecessary final here):

    Baz baz = new Baz("one", "two", injector);

  4. Add the injector to the declaration of the Baz constructor, to receive it from the main method:

    public Baz(String one, String two, Injector injector) {

  5. Add an injection into boofPanel to the Baz constructor, right after the new BoofPanel line, to get Guice to inject BoofPanel:

    injector.injectMembers(boofPanel);

That's it.

七七 2025-01-13 05:53:22

您可能想要

public class BoofPanel extends JPanel {
  private FooInterface fooI;
  @Inject BoofPanel(FooInterface foo) {
     this.fooI = foo;
  }
  public void doSomething() {
    fooI.getBar();
  }
}

,然后在主程序中您可能需要类似 injector.createInstance(BoofPanel.class) 的东西,它会使用 MockFooImpl 自动满足 FooInterface 要求

You probably want

public class BoofPanel extends JPanel {
  private FooInterface fooI;
  @Inject BoofPanel(FooInterface foo) {
     this.fooI = foo;
  }
  public void doSomething() {
    fooI.getBar();
  }
}

and then in your main you probably want something like injector.createInstance(BoofPanel.class) and it'll automatically fulfill the FooInterface requirement with a MockFooImpl.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文