2

I have the following declarative service:

@Component(
    immediate = false,
    property={"propA=valueA","propB=valueB","propC=valueC"},
    scope=ServiceScope.SINGLETON
)
public class ServiceImpl implements ServiceI{...}

And this the code I do to find this service(manually) by propA:

String filter = "(&(objectClass=" + ServiceI.class.getName() + ")(propA=valueA))";
ServiceReference[] serviceReferences = bundleContext.getServiceReferences((String)null,filter);
ServiceI service=(ServiceI) bundleContext.getService(serviceReferences[0]);

How can I get valueB of propB and valueC of propC of found service?

Pavel_K
  • 10,748
  • 13
  • 73
  • 186

2 Answers2

4

You can use the getProperty of a ServiceReference instance :

Object propBValue = serviceReference.getProperty("propB");
Jérémie B
  • 10,611
  • 1
  • 26
  • 43
2

Slightly out of scope. With annotations the code would look like:

 @Reference(target="(propA=valueA)")
 void setI(ServiceI s, Map<String,Object> properties) {
    String propB = properties.get("propB");
    String propC = properties.get("propC");
    ...
 }
Peter Kriens
  • 15,196
  • 1
  • 37
  • 55