Ger*_*ies 7 java user-interface swing dependency-injection guice
我有一个Swing应用程序,我想从意大利面转换为使用Guice的依赖注入.使用Guice提供配置和任务队列等服务非常好,但我现在开始使用应用程序的GUI,并且不确定如何继续.
该应用程序基本上是一个JFrame带有一堆标签的应用程序JTabbedPane.每个选项卡都是一个单独的JPanel子类,它布置了各种组件,并且需要服务来在按下某些按钮时执行操作.
在当前的应用程序中,这看起来像这样:
@Inject
public MainFrame(SomeService service, Executor ex, Configuration config) {
tabsPane = new JTabbedPane();
// Create the panels for each tab and add them to the tabbedpane
somePanel = new SomeTabPanel(service, ex, config);
tabsPane.addTab("Panel 1", somePanel);
someOtherPanel = new SomeOtherTabPanel(service, ex, config);
tabsPane.addTab("Panel 2", someOtherPanel);
... do more stuff
}
Run Code Online (Sandbox Code Playgroud)
显然,这并不完全遵循DI最佳实践.我不想拥有@Inject标签,因为这会让我得到一个包含许多参数的构造函数.我确实希望使用Guice将所需的依赖项注入到我需要的任何选项卡对象中,而不必将所有这些依赖项传递给选项卡构造函数.
所有依赖于标签的对象是我服务的Module知道,所以基本上所有我想我想要做的就是要求吉斯为所需对象,并让他们构建了我.
| 归档时间: |
|
| 查看次数: |
2382 次 |
| 最近记录: |