Osgi 如何通过从片段中读取值来设置组件中的最终静态变量值?
我只是OSGi的初学者,我们仍在使用版本4。 我有一个OSGi组件,其中一个类具有公共静态final(psf)变量 我想做的是,我想使用一个片段,它从属性文件中读取值,并在组件中设置psf变量的值?如果未找到片段,则应将值设置为默认值 请查找我的快照代码,并告诉我如何执行此操作 组件类Osgi 如何通过从片段中读取值来设置组件中的最终静态变量值?,osgi,osgi-bundle,osgi-fragment,Osgi,Osgi Bundle,Osgi Fragment,我只是OSGi的初学者,我们仍在使用版本4。 我有一个OSGi组件,其中一个类具有公共静态final(psf)变量 我想做的是,我想使用一个片段,它从属性文件中读取值,并在组件中设置psf变量的值?如果未找到片段,则应将值设置为默认值 请查找我的快照代码,并告诉我如何执行此操作 组件类 public final class OdsPrincipals { /*******************************************************************
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);
}
} ```
您希望在运行时设置所谓的编译时常量。根据定义,这是不可能的。原因是,在编译时,代码中出现的每一个变量都会替换为常量值。因此,即使您可以在运行时更改它们,编译后的其余代码也不会更新。谢谢您的建议。我找到了另一种解决问题的方法。