我只是 OSGi 的初学者,我们仍在使用版本 4。我有一个 OSGi 组件,其中一个类具有公共静态最终 (psf) 变量。
我想做的是,我想使用一个片段,它从属性文件中读取值并设置组件中 psf 变量的值。? 如果未找到片段,则应将值设置为默认值。
请找到我的快照代码,让我知道我该怎么做?
组件类
public final class OdsPrincipals {
/*****************************************************************************************
* Static/Inner class members
******************************************************************************************/
private static final String ODS_PRODUCT_NAME;
private static final String ODS_PRINCIPAL_NAME;
static {
//How to set the values of static final variables.
}
片段类
public class OdsPrincipalProperties {
/*'***************************************************************************************
* Static/Inner class members
******************************************************************************************/
protected static final String ODS_PRINCIPAL_PROPERTIES_FILE = "odsprincipal.properties";
private static final Properties properties = new Properties();
static {
try {
properties.load(
OdsPrincipalProperties.class.getResourceAsStream(ODS_PRINCIPAL_PROPERTIES_FILE));
} catch (Exception e) {
ServiceLogger.error(e);
} finally {
}
}
private static final OdsPrincipalProperties odsPrincipalProperties = new OdsPrincipalProperties();
public static OdsPrincipalProperties getInstance() {
return odsPrincipalProperties;
}
/*'***************************************************************************************
* Class members
******************************************************************************************/
protected OdsPrincipalProperties() {
}
/*
* returns the value for a given key. If the key is not
* found, returns the default value.
*
*/
public String getValue(String key, String defaultValue) {
return properties.getProperty(key, defaultValue);
}
} ```