首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Eclipse 4注入OSGI服务

Eclipse 4注入OSGI服务
EN

Stack Overflow用户
提问于 2014-06-11 11:55:22
回答 1查看 1.9K关注 0票数 1

我希望将Settings类转换为e4可以注入的OSGI声明性服务。

我已经在OSGI/setingsService.xml中创建了该服务。

代码语言:javascript
复制
<?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/

代码语言:javascript
复制
@Inject
private IPrivacySettingsService privacySettingsService;

但是我得到了一个NullPointerException

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-12 09:47:05

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

您需要使用ContextInjectionFactory来创建类:

代码语言:javascript
复制
ApprovalDialogJob job = new ApprovalDialogJob(extensionReader);
ContextInjectionFactory.inject(job, eclipseContext);

其中eclipseContext是IEclipseContext的一个实例,您可以通过将它注入Startup或使用:

代码语言:javascript
复制
BundleContext bundleContext = FrameworkUtil.getBundle(Startup.class).getBundleContext();
IEclipseContext context = EclipseContextFactory.getServiceContext(bundleContext);

希望这能有所帮助。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24162328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档