[英]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.