Eclipse RCP 4通过声明式服务使用捆绑软件 [英] Eclipse RCP 4 use bundle via declarative service

查看:93
本文介绍了Eclipse RCP 4通过声明式服务使用捆绑软件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个OSGi捆绑包,以在eclipse 4 rcp应用程序中使用它.如果我添加依赖项,将这些服务注册到激活器中,然后将其注入到我的类中,则该服务的用法可以正常工作.

I have written an OSGi bundle to use it in my eclipse 4 rcp application. The usage of the service works fine if I add the dependencies, register these service in my activator and inject it in my class.

在激活器中

IUserService service = new TestUserService();
context.registerService(IUserService.class.getName(), service, null);

在我班上

@Inject
IUserService service;

service.getSth();

我读到通过声明式服务使用捆绑包是更好的方法.所以改变了我的实现. 我在捆绑包中创建了一个组件定义以提供服务:

I read that using bundles via declarative services should be the better way. So changed my implementation. I created a component definition in my bundle to provide my service:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="usermanagement.test">
   <implementation class="usermanagement.test.TestUserService"/>
   <service>
      <provide interface="usermanagement.IUserService"/>
   </service>
</scr:component>

然后,我从激活器中删除了服务注册,并创建了一个服务使用者类:

Then I removed the service registration from my activator and created an service consumer class:

public class UserServiceConsumer {

    private IUserService service;

    public synchronized void setQuote(IUserService service) {
        this.service = service;
    }

    public synchronized void unsetQuote(IUserService service) {
        if (this.service == service) {
            this.service = null;
        }
    }

}

和另一个组件定义:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="UserServiceConsumer">
   <implementation class="services.UserServiceConsumer"/>
   <reference bind="setService" cardinality="1..1" interface="usermanagement.IUserService" name="IUserService" policy="static" unbind="unsetService"/>
</scr:component>

经过这些修改后,我的私人服务注入不再起作用.问题在于注入的服务引用每次都是NULL.

After these modifications the injection of my serivce does not work anymore. The problem is that the injected service reference is NULL everytime.

有人知道为什么吗?我忘了什么吗?

Does anyone know why? Did I forgot something?

非常感谢!

推荐答案

我可以建议您做一些调试工作.

I can suggest a few things you can do to debug.

  1. 您实际上在运行时中得到了一个scr实现吗? Equinox核心未包含SCR(声明性服务的另一个名称),因此您需要将其包括在内.大多数人使用Felix SCR套件-它会非常幸福地坐在Equinox的顶部.

  1. Have you actually got an scr implementation in your runtime? SCR (another name for declarative services) isn't included in Equinox core, so you'll need to include it. Most people use the Felix SCR bundle - it will sit very happily on top of Equinox.

由于声明式服务仅使用服务,因此您可以一次更改应用的一半,以识别是服务使用或注册不起作用.

Since Declarative Services just use services, you can change one half of your app at a time, to identify whether it's the service consumption or registration which isn't working.

您还可以使用Equinox控制台检查服务注册.使用"ss"标识您的捆绑软件,然后使用"bundle [no]"查看注册和使用了哪些服务.如果您使用的是Felix SCR,则还提供了Equinox控制台扩展,因此您可以使用"scr列表"查看捆绑尝试注册的所有服务(及其状态),并使用"scr信息"以获取有关特定特定内容的更多详细信息.服务.

You can also use the Equinox console to inspect your service registration. Use 'ss' to identify your bundle, then 'bundle [no]' to see what services are registered and consumed. If you're using Felix SCR, there are also Equinox console extensions, so you can use 'scr list' to see all services which a bundle attempts to register (and their state), and 'scr info' for more details on a particular service.

这篇关于Eclipse RCP 4通过声明式服务使用捆绑软件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆