簡體   English   中英

Eclipse 4注入OSGI服務

[英]Eclipse 4 Injecting an OSGI Service

我想將Settings類變成e4可以注入的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引擎將不會管理對象,因此不會注入任何值。

您需要使用ContextInjectionFactory創建您的類:

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

凡eclipseContext是一個實例IEclipseContext ,您可以通過將其注入獲得Startup或使用:

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

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM