我想将一个 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/