3

我正在开发一个使用 Google Guice 作为 IOC 容器的 Java Swing 应用程序。我直接注入组件,但这让 Guice在 EDT 之外创建组件。

该应用程序看起来有点像这样:

private Panel1 panel1;
private Panel2 panel2;

@Inject
public class Application(Panel1 panel1, Panel2 panel2) {
    this.panel1 = panel1;
    this.panel2 = panel2;
}

看看这里这里的问题,我得出了注入加载程序而不是直接注入组件的结论。

private PanelLoader1 loader1;
private PanelLoader2 loader2;

private Panel1 panel1;
private Panel2 panel2;

@Inject
public class Application(PanelLoader1 loader1, PanelLoader2 loader2) {
    this.loader1 = loader1;
    this.loader2 = loader2;

    loader1.load();
    loader2.load();

    this.panel1 = loader1.get();
    this.panel2 = loader2.get();
}

public class PanelLoader {
    private Panel panel;
    public void load() {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                panel = new Panel();
            }
        });
    }
    public Panel get() {
        return panel;
    }
}

它是否正确?这样做有什么最佳做法吗?

4

1 回答 1

4

如果你想使用“加载器”,你应该实现com.google.inject.Provider. 查看http://code.google.com/p/google-guice/wiki/InjectingProviders上的示例

您不需要自己注入提供者,您可以配置模块以注入提供者创建的对象:

public class PanelModule extends AbstractModule {

@Override
protected void configure() {
    bind(Panel1.class).toProvider(Panel1Provider.class);
}

private static class Panel1Provider implements Provider<Panel1> {

    private Panel1 panel1;

    @Override
    public Panel1 get() {
        try {
            EventQueue.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    panel1 = new Panel1();
                }
            });
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e); // should not happen
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
    return panel1;
}

或者,如果每个组件只需要一个实例,则可以将实例直接绑定到类型:

public class PanelModule extends AbstractModule {

Panel1 panel1;
Panel2 panel2;

@Override
protected void configure() {

    try {
        EventQueue.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                panel1 = new Panel1();
                panel2 = new Panel2();
            }
        });
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e); // should not happen
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }

    bind(Panel1.class).toInstance(panel1);
    bind(Panel2.class).toInstance(panel2);
}
于 2012-05-18T21:07:34.500 回答