【问题标题】:using properties within the properties file使用属性文件中的属性
【发布时间】:2012-01-15 12:04:30
【问题描述】:

我为标题道歉。我找不到更好的方式来解释这种情况。

我使用 URL http://www.exampledepot.com/egs/java.util/Props.html 中描述的 Property 类来加载属性文件

我的问题是我可以使用该属性文件中的属性吗?

示例:

test.properties

 url.main="http://mysite.com"
    url.games={url.main}/games
    url.images={url.main}/images
    .
    .
    .

用其他语法可以吗?

谢谢

【问题讨论】:

标签: java properties


【解决方案1】:

Apache Commons Configuration 为此提供:http://commons.apache.org/configuration/

加载配置文件的简单示例代码:

Configuration config = new PropertiesConfiguration("config.properties");

您可以拥有“变量插值”属性,如此处所述http://commons.apache.org/configuration/userguide/howto_basicfeatures.html#Variable_Interpolation

application.name = Killer App
application.version = 1.6.2

application.title = ${application.name} ${application.version}

它还允许您在使用时包含其他配置文件:

include = colors.properties
include = sizes.properties

除了一系列其他功能。

【讨论】:

    【解决方案2】:

    以前从未见过。当然,您可以制作自己的预处理器。只要引用的属性出现在对其的任何引用之前,您就应该能够使用一些正则表达式/字符串替换来实现它。但是:我不推荐这种方法。

    通过定义不同的属性更好地解决重复问题:

    1. 将:url.games={url.main}/games 更改为 url.games_extension=/games
    2. 添加:url.mainurl.games_extension 以在您的应用程序代码中获取完整的游戏 URL。

    【讨论】:

      【解决方案3】:

      我编写了自己的配置库,它支持属性文件中的变量扩展,have a look,看看它是否提供了你需要的东西。我写的一篇介绍该功能的文章是here

      【讨论】:

        【解决方案4】:

        没有直接的方法来替换属性文件/对象中的属性值,但是您可以通过getProperty() 方法读取后替换属性值。要生成串联消息 - 请查看 MessageFormat 类。

        String baseValue=prop.getProperty("url.main");
        

        【讨论】:

          【解决方案5】:

          我做过类似的事情,这并不难,您只需将 Properties 类子类化并实现自己的 getProperty 方法,检查模式并在必要时替换它。

          //this makes the pattern ${sometext}
          static public final String JAVA_CONFIG_VARIABLE = "\\$\\{(.*)\\}";
          
          @Override
          public String getProperty(String key)
          {
              String val = super.getProperty(key);
              if( val != null && val.indexOf("${") != -1 )
              {
                  //we have at least one replacable parm, let's replace it
                  val = replaceParms(val);
              }
              return val;
           }
          
           public final String replaceParms(String in)
           {
               if(in == null) return null;
               //this could be precompiled as Pattern is supposed to be thread safe
               Pattern pattern = Pattern.compile(JAVA_CONFIG_VARIABLE);
               Matcher matcher = pattern.matcher(in);
               StringBuffer buf = new StringBuffer();
               while (matcher.find())
               {
                   String replaceStr = matcher.group(1);
                   String prop = getProperty(replaceStr);   
                   //if it isn't in our properties file, check if it is a system property
                   if (prop == null )
                       prop = System.getProperty(replaceStr);
                   if( prop == null )
                   {
                      System.out.printf("Failed to find property '%s' for '%s'\n", replaceStr, in);
                   }
                   else
                   {
                     matcher.appendReplacement(buf, prop);
                   } 
               }
               matcher.appendTail(buf);
               String result = buf.toString();
               return result;
           }
          

          【讨论】:

            猜你喜欢
            • 2016-09-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-11-15
            • 1970-01-01
            • 2012-10-21
            • 1970-01-01
            相关资源
            最近更新 更多