我不确定我是否完全理解依赖注入背后的想法,特别是使用Guice.
我有很大的摇摆应用程序,我想引入guice,来解耦这个应用程序.假设我在主要班级有注射器
Guice.createInjector(new BindingModule());
Application app = injector.getInstance(Application.class);
app.run();
Run Code Online (Sandbox Code Playgroud)
它有效.如果我有一些字段,让我们说在应用程序类中的JPanel,用@Inject注释然后注入它.但是如果我在Application构造函数中手动创建一些东西,那么将不会注入示例中的JTree(假设一切都配置正确).
class Application {
@Inject JPanel mainPanel //this will be injected
JPanel otherPanel;
public Application() {
otherPanel = new MyNewPanel();
mainPanel.add(otherPanel);
}
}
class MyNewPanel extends JPanel {
@Inject JTree tree; //this will not be injected
public MyNewPanel() {
add(tree);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,我是否需要控制注射的所有注射物体.我不能像我一样打破控制otherPanel.
| 归档时间: |
|
| 查看次数: |
4325 次 |
| 最近记录: |