【发布时间】:2014-06-20 17:13:01
【问题描述】:
我有一些类似这样的代码来读取可以使用 sling:OsgiConfig 节点设置或在 Felix UI 中设置后设置的值...
@Component(immediate = true, metatype = true, label = "Dummy Service")
public class DummyService {
@Property(label = "Dummy Service Value")
public static final String DUMMY_VALUE = "dummyValue";
private static String m_strDummyValue = "default value";
public static String getDummyValue(){
return m_strDummyValue;
}
@Activate
protected void activate(ComponentContext context) {
configure(context.getProperties());
}
@Deactivate
protected void deactivate(ComponentContext context) {
}
@Modified
protected void modified(ComponentContext componentContext) {
configure(componentContext.getProperties());
}
public void updated(Dictionary properties) throws ConfigurationException {
configure(properties);
}
private void configure(Dictionary properties) {
m_strDummyValue = OsgiUtil.toString(properties.get(DUMMY_VALUE), null);
}
}
并且可以在任何消费类中调用
DummyService.getDummyValue();
这目前在我们的开发环境中工作。它也与其他供应商编写的一些代码非常相似,目前正在客户端环境中生产,并且似乎正在运行。但是,我遇到了这篇文章OSGi component configurable via Apache Felix...,它建议不要使用这样的静态访问器。是否存在 getDummyValue() 可能返回不正确值的潜在问题,还是建议更多地与 OSGi 的模式在哲学上保持一致?
【问题讨论】: