在OSGi DS服务上使用@Reference时组件创建失败

在OSGi DS服务上使用@Reference时组件创建失败,osgi,eclipse-rcp,declarative-services,Osgi,Eclipse Rcp,Declarative Services,我正在尝试在DS组件上调用绑定/取消绑定方法。我已经把它简化为一个最简单的例子,但它不起作用 如果我删除bind方法上的@Reference,那么测试就成功了。显然,日志语句不会被调用。否则,它将在AssertNotNull上失败 对我做错了什么有什么建议吗?向组件添加绑定/解除绑定方法的正确方法是什么 更新代码以显示正确的方法。 接口 public interface Foo { public abstract String bar(); } 阶级 生成的组件定义 <?xml

我正在尝试在DS组件上调用绑定/取消绑定方法。我已经把它简化为一个最简单的例子,但它不起作用

如果我删除bind方法上的@Reference,那么测试就成功了。显然,日志语句不会被调用。否则,它将在AssertNotNull上失败

对我做错了什么有什么建议吗?向组件添加绑定/解除绑定方法的正确方法是什么

更新代码以显示正确的方法。

接口

public interface Foo {
    public abstract String bar();
}
阶级

生成的组件定义

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" activate="bindFoo" deactivate="unbindFoo" enabled="true" immediate="true" name="com.vogelware.experiment.FooImpl">
   <service scope="singleton">
      <provide interface="com.vogelware.experiment.Foo"/>
   </service>
   <implementation class="com.vogelware.experiment.FooImpl"/>
</scr:component>

测试班

class FooTest {
    @Test
    void test() throws InterruptedException {
        ServiceTracker<Foo, Foo> testTracker = new ServiceTracker<Foo, Foo>(Activator.getContext(), Foo.class, null);
        testTracker.open();
        testTracker.waitForService(500);
        Foo user = testTracker.getService();
        testTracker.close();
        assertNotNull(user);  <= fails here
    }
}
class-FooTest{
@试验
void test()抛出InterruptedException{
ServiceTracker testTracker=新的ServiceTracker(Activator.getContext(),Foo.class,null);
testTracker.open();
testTracker.waitForService(500);
Foo user=testTracker.getService();
testTracker.close();

assertNotNull(user);您的组件正试图将自身用作依赖项。因此,您有一个无法解决的循环引用。只有在组件依赖于一个Foo服务(@reference)之前,您的组件才能得到满足(并因此注册为一个Foo服务)可以满足。

您的组件正试图将自身用作依赖项。因此,您有一个无法解决的循环引用。只有在组件依赖于一个Foo服务(@reference)之前,您的组件才能满足要求(并因此注册为一个Foo服务)可以满足。

我的问题的答案是,在1.3规范中,使用@Activate和@Deactivate。我修改了原始问题,以代码形式显示解决方案


查看更多详细信息。

我的问题的答案是,在1.3规范中,使用@Activate和@Deactivate。我修改了原始问题,以代码形式显示解决方案


查看更多详细信息。

好的,这很有意义。在组件上指定绑定/取消绑定方法的正确方法是什么?好的,这很有意义。在组件上指定绑定/取消绑定方法的正确方法是什么?
class FooTest {
    @Test
    void test() throws InterruptedException {
        ServiceTracker<Foo, Foo> testTracker = new ServiceTracker<Foo, Foo>(Activator.getContext(), Foo.class, null);
        testTracker.open();
        testTracker.waitForService(500);
        Foo user = testTracker.getService();
        testTracker.close();
        assertNotNull(user);  <= fails here
    }
}