我有以下声明性服务:
@Component(
immediate = false,
property={"propA=valueA","propB=valueB","propC=valueC"},
scope=ServiceScope.SINGLETON
)
public class ServiceImpl implements ServiceI{...}
这是我通过propA(手动)找到这项服务的代码:
String filter = "(&(objectClass=" + ServiceI.class.getName() + ")(propA=valueA))";
ServiceReference[] serviceReferences = bundleContext.getServiceReferences((String)null,filter);
ServiceI service=(ServiceI) bundleContext.getService(serviceReferences[0]);
如何获得propB的valueB和找到的服务的propC的valueC?
答案 0 :(得分:4)
您可以使用getProperty
实例的ServiceReference
:
Object propBValue = serviceReference.getProperty("propB");
答案 1 :(得分:2)
稍微超出范围。使用注释代码看起来像:
@Reference(target="(propA=valueA)")
void setI(ServiceI s, Map<String,Object> properties) {
String propB = properties.get("propB");
String propC = properties.get("propC");
...
}