如何引用实现给定接口的所有组件?
How can I reference all components that implement a given interface?
我在 Eclipse Equinox OSGi 环境中使用 Apache Felix 服务组件运行时 (SCR)。
声明了几个实现接口 org.example.Producer
的组件,例如:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.example.ProducerA">
<implementation class="org.example.ProducerA"/>
<service>
<provide interface="org.example.Producer"/>
</service>
</scr:component>
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.example.ProducerB">
<implementation class="org.example.ProducerB"/>
<service>
<provide interface="org.example.Producer"/>
</service>
</scr:component>
现在在另一个组件中,我想引用所有那些动态实现接口 org.example.Producer
的组件:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.example.ConsumerA">
<implementation class="org.example.ConsumerA"/>
<reference bind="bindProducer" cardinality="0..n" interface="org.example.Producer" policy="dynamic" unbind="unbindProducer"/>
<service>
<provide interface="org.example.Consumer"/>
</service>
</scr:component>
但这会在运行时出错。 SCR 似乎在其搜索过滤器中包含了一个组件名称:
!ENTRY org.eclipse.equinox.ds 1 0 2015-06-22 11:31:31.781
!MESSAGE Could not bind a reference of component org.example.ConsumerA. The reference is: Reference[name = org.example.Producer, interface = org.example.Producer, policy = dynamic, cardinality = 0..n, target = null, bind = bindProducer, unbind = unbindProducer]
如您在错误消息中所见,它正在明确搜索名称为 org.example.Producer
的组件。但是上面列出的 none 个组件具有该名称(org.example.ProducerA
、org.example.ProducerB
)。
所以问题是我如何通过忽略名称动态引用为给定接口提供实现的组件?
正如 Neil Bartlett 指出的那样,我被提到的日志消息误导了。只是对应的服务启动时间很长,最后都绑定正确了。
我在 Eclipse Equinox OSGi 环境中使用 Apache Felix 服务组件运行时 (SCR)。
声明了几个实现接口 org.example.Producer
的组件,例如:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.example.ProducerA">
<implementation class="org.example.ProducerA"/>
<service>
<provide interface="org.example.Producer"/>
</service>
</scr:component>
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.example.ProducerB">
<implementation class="org.example.ProducerB"/>
<service>
<provide interface="org.example.Producer"/>
</service>
</scr:component>
现在在另一个组件中,我想引用所有那些动态实现接口 org.example.Producer
的组件:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.example.ConsumerA">
<implementation class="org.example.ConsumerA"/>
<reference bind="bindProducer" cardinality="0..n" interface="org.example.Producer" policy="dynamic" unbind="unbindProducer"/>
<service>
<provide interface="org.example.Consumer"/>
</service>
</scr:component>
但这会在运行时出错。 SCR 似乎在其搜索过滤器中包含了一个组件名称:
!ENTRY org.eclipse.equinox.ds 1 0 2015-06-22 11:31:31.781
!MESSAGE Could not bind a reference of component org.example.ConsumerA. The reference is: Reference[name = org.example.Producer, interface = org.example.Producer, policy = dynamic, cardinality = 0..n, target = null, bind = bindProducer, unbind = unbindProducer]
如您在错误消息中所见,它正在明确搜索名称为 org.example.Producer
的组件。但是上面列出的 none 个组件具有该名称(org.example.ProducerA
、org.example.ProducerB
)。
所以问题是我如何通过忽略名称动态引用为给定接口提供实现的组件?
正如 Neil Bartlett 指出的那样,我被提到的日志消息误导了。只是对应的服务启动时间很长,最后都绑定正确了。