java - Eclipse 4 注入(inject) OSGI 服务

标签 java eclipse dependency-injection e4

我想将一个 Settings 类变成一个 e4 可以注入(inject)的 OSGI 声明式服务。

我在 OSGI-INF/settingsService.xml 中创建了服务:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.recommenders.privacy.rcp">
        <implementation class="org.eclipse.recommenders.privacy.rcp.PrivacySettingsService"/>
    <service>
        <provide interface="org.eclipse.recommenders.privacy.rcp.IPrivacySettingsService"/>
    </service>
</scr:component>

我已经用 @Inject 注释了变量,如下所述: http://toedter.com/2010/06/28/eclipse-4-0-dependency-injection-and-osgi-declarative-services/

@Inject
private IPrivacySettingsService privacySettingsService;

但是我得到了一个NullPointerException

最佳答案

查看您的代码,您的问题似乎是您正在使用 new 运算符创建 ApprovalDialogJob。这样 DI 引擎将不会管理对象,因此它不会注入(inject)任何值。

您需要使用 ContextInjectionFactory 来创建您的类:

ApprovalDialogJob job = new ApprovalDialogJob(extensionReader);
ContextInjectionFactory.inject(job, eclipseContext);

其中 eclipseContext 是 IEclipseContext 的一个实例,您可以通过将其注入(inject) Startup 或使用以下方式获得:

BundleContext bundleContext = FrameworkUtil.getBundle(Startup.class).getBundleContext();
IEclipseContext context = EclipseContextFactory.getServiceContext(bundleContext);

希望这对您有所帮助。

关于java - Eclipse 4 注入(inject) OSGI 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24162328/

相关文章:

java - @Categories Junit 4.11 的继承

eclipse - 执行目标tomcat失败:deploy Error writing to server

linux - centos 6安装的eclipse无法运行jsp项目

用 @Creatable 注释的 Java 类在注入(inject)其他插件类时缺少属性

wcf - 带 wcf 的 Autofac

java - 迭代连接整数数组

java - 如果节点的元素等于最大元素,如何删除整个节点

JavaFX eclipse 使用 wix 自定义部署的包

java - 使用 Java Web App 中的 (Scala) 库,获取 NoClassDefFoundError

jsf - WELD-001408 : Unsatisfied dependencies for type Customer with qualifiers @Default