0

我只是 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);
   }

} ```
4

1 回答 1

1

您想在运行时设置所谓的编译时常量。根据定义,这是不可能的。原因是在编译时,代码中每次出现的变量都会被常量的值替换。因此,即使您可以在运行时更改它们,您编译的其余代码也不会更新。

于 2019-06-14T07:12:08.283 回答