【问题标题】:IPojo - @Reference does not inject serviceIPojo - @Reference 不注入服务
【发布时间】:2011-12-21 15:54:39
【问题描述】:

在一个bundle中有接口IService,由ServiceImpl类实现:

public interface IService
{
    void doSomething();
}

@Component
@Provides
@Instantiate
public class ServiceImpl implements IService
{
    public void doSomething()
    {
    }
}

在第二个包中,我有另一个类 ServiceConsumer(一个 GoGo shell 命令;不包括特定注释),使用第一个包中提供的服务:

@Component
@Provides
@Instantiate
public class ServiceConsumer
{
    @Requires
    private IService service;

    public doIt()
    {
        service.doSomething();
    }

}

当我在 Felix 中导入并启动这两个包时,我可以看到我的所有服务都使用 ipojo:instances 正确实例化,并且 ServiceImpl 提供了 IService。但是执行doIt()时,service为null。

由于 IService 似乎可用,我希望 @Requires 注入好的实例,但似乎没有这样做。

我感觉有一些很明显的事情我没有做,但我不知道是什么。

【问题讨论】:

  • 我想知道这里是否存在服务兼容性问题。捆绑包是如何排列的?提供者包和消费者包是否都从同一个地方导入接口包?

标签: java osgi apache-felix


【解决方案1】:

您是否还创建了 metadata.xml? 请在此处查看示例http://felix.apache.org/site/ipojo-in-10-minutes.html

afaik 你也可以使用 maven 插件生成它 看这里http://felix.apache.org/site/ipojo-hello-word-maven-based-tutorial.html

【讨论】:

    【解决方案2】:

    'doIt()'方法怎么叫?

    您不需要 XML 文件,因为您正在使用注释。

    如果您安装了“arch”命令,您可以检查所有实例是否已声明且有效:

    ipojo:instances
    ipojo:instance instance_name
    

    问候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-16
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多