OSGI:从DS获取房产信息

时间:2016-08-07 16:47:24

标签: java osgi declarative-services

我有以下声明性服务:

@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?

2 个答案:

答案 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");
    ...
 }