0

If I want to have a service injected in iPojo using method injection I need to do

@Bind
public void bindService(MyService implementation) {
}

Based on the type of the argument it knows which impl to inject just based on the interface. If there are two impls of the same interface it will choose based on some algorithm (service rank?)

Now, if I need multiple intances of the service that need to be created on demand based on an event, I understand I'm supposed to use org.apache.felix.ipojo.Factory and construct the instances through it. The problem I have is that I have not found a way to specify which factory to have injected using only the Interface of the instances of the factory

In other words

@Bind
public void bindService(org.apache.felix.ipojo.Factory myFactory) {
}

is ambiguous. The only way I have found to get the factory injected is using @Bind(filter="(factory.name=myServiceFactoryImpl)" but this couples the consumer to a concrete provider, which defeats the whole point of OSGi services. What I want to do is in plain english "bind me to a factory whose instances implement the interface MyService". If there are again many providers' factories of the same interface, it should use the same disambiguation mechanism as when injecting instances directly. Is this possible?

4

2 回答 2

2

component.providedServiceSpecifications只需使用列出由创建的实例公开的接口的属性来使用过滤器:

@Requires(filter="(component.providedServiceSpecifications=org.acme.Foo)")
Factory[] factories;
于 2014-12-13T09:07:55.767 回答
1

MyServiceFactory只需像使用一种方法一样使用工厂接口MyService create();。然后编写 MyServiceFactory 的实现并将其发布为 OSGi 服务。然后,客户端可以绑定 MyServviceFactory 接口并创建他的服务实例。

于 2014-12-12T08:00:45.200 回答