Eclipse RCP 4通过声明性服务使用bundle

ros*_*lze 5 osgi eclipse-rcp declarative-services

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

在激活者

IUserService service = new TestUserService();
context.registerService(IUserService.class.getName(), service, null);
Run Code Online (Sandbox Code Playgroud)

在我的班上

@Inject
IUserService service;

service.getSth();
Run Code Online (Sandbox Code Playgroud)

我读到通过声明性服务使用bundle应该是更好的方法.所以改变了我的实施.我在我的包中创建了一个组件定义来提供我的服务:

<?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>
Run Code Online (Sandbox Code Playgroud)

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

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;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

和另一个组件定义:

<?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>
Run Code Online (Sandbox Code Playgroud)

在这些修改之后,我的服务的注射不再起作用.问题是每次注入的服务引用都是NULL.

有谁知道为什么?我忘记了什么吗?

非常感谢!

Hol*_*ins 3

我可以建议您进行一些调试操作。

  1. 你真的在运行时实现了 scr 吗?SCR(声明性服务的另一个名称)不包含在 Equinox 核心中,因此您需要包含它。大多数人都使用 Felix SCR 捆绑包 - 它会很高兴地放在 Equinox 之上。

  2. 由于声明式服务仅使用服务,因此您可以一次更改应用程序的一半,以确定是服务消耗还是注册不起作用。

  3. 您还可以使用 Equinox 控制台检查您的服务注册。使用“ss”来标识您的捆绑包,然后使用“捆绑包[否]”来查看注册和使用的服务。如果您使用 Felix SCR,还有 Equinox 控制台扩展,因此您可以使用“scr list”查看捆绑包尝试注册的所有服务(及其状态),并使用“scr info”了解有关特定服务的更多详细信息服务。