【问题标题】:In the OSGi/iPOJO world, how to get a list of instances of classes implementing an interface?在 OSGi/iPOJO 世界中,如何获取实现接口的类的实例列表?
【发布时间】:2015-09-29 11:18:56
【问题描述】:

在 JavaEE/CDI 世界中,我知道如何获得实现给定接口的类的实例列表:通过使用 Instance<MyInterface> 结合 reflections 库。

但是在 OSGI/iPOJO 的世界里,该怎么做呢?

我知道我通过使用@Requires MyInterface anInstance 获得了一个实例。但是我怎样才能以编程方式访问所有这些类?

【问题讨论】:

    标签: java osgi inject reflections ipojo


    【解决方案1】:

    只需使用BundleContext

    @Context BundleContext context;
    
    public List<ServiceReference> getInstancesImplementingX() {
        return context.getServiceReferences(X.class);   
    }
    

    【讨论】:

    • 不幸的是,这要求我加载的所有类都是服务,这是我没想到的......
    • 怎么样,有办法不用加载类:context.getServiceReferences("name.of.my.class", null);它返回一个数组,如果没有服务匹配,则返回 null。
    猜你喜欢
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 2011-04-28
    • 2010-09-15
    • 2013-06-05
    • 1970-01-01
    相关资源
    最近更新 更多