Osgi declarative service conditional binding

I have this scenario, I have three declarative services that provide the same interface (say, the reader interface, and I have readimpl1-database-readerimpl2-flat file-readerimpl3-memory). I want to have a consumer who only contacts the database implementation. In the definition of a component, we give it a name, so I'm sure that the name is in the registry, so if I need to add an activation method, I can search for it from the component context using the name.

I want to try using bind / unbind using the service name as a parameter. I am pretty sure that the β€œtarget” parameter in the component reference element can be used for this, but I have not found how to use it.

Has anyone else done this?

It will be like using @Reference (mapped- name = "Foo")

+8
osgi declarative-services
source share
1 answer

The goal is just an OSGi filter . You can use it to filter using any service property. So, if your services have a property called backend with the values file or database , you can associate with the following purpose:

 <scr:reference ... target="(backend=database)"/> 

And the service with the database will be registered as:

 <scr:component ...> ... <property name="backend" type="String" value="database"/> </scr:component> 
+14
source share

All Articles