有人可以解释一下如何使用 Eclipse E4 DI 功能来注入(inject)我的自定义服务/提供程序吗?
@Singleton
public class ConnectionPool {
// should be a singleton
}
public class MyService implements IMyService {
@Inject
ConnectionPool pool;
}
// referenced in application model
public class SamplePart {
@Inject
IMyService myService;
@PostConstruct
public void createComposite(Composite parent) {
myService.doSomething();
}
}
或者是否可以将 Google Guice(例如)与 Eclipse DI 一起使用?
我尝试了几个例子(例如 this ),但它对我不起作用
最佳答案
您需要将@Creatable
注释添加到ConnectionPool
(org.eclipse.e4.core.di.annotations.Creatable
):
@Singleton
@Creatable
public class ConnectionPool
您还可以使用 OSGi 服务来实现该服务。还可以在 AddOn 或应用程序 LifeCycle 类中创建服务。您还可以使用 ContextFunction
创建服务。
关于java - 如何在 E4 应用程序中注入(inject)自定义单例 bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26271800/