using properties within the properties file

前端 未结 5 1089
执念已碎
执念已碎 2020-12-17 02:14

I apologize for the title.. i couldn\'t find a better way to explain the situation.

I use to load properties file using a Property class as described in the URL http

相关标签:
5条回答
  • 2020-12-17 02:50

    Never seen that before. You could make your own preprocessor of course. As long as the referenced property occurs before any references to it, you should be able to implement it using some regular expressions/string replacement. But: I would not recommend this method.

    Better resolve the duplication by defining different properties:

    1. change: url.games={url.main}/games into url.games_extension=/games
    2. prepend: url.main to url.games_extension to get the full games url in your application code.
    0 讨论(0)
  • 2020-12-17 02:51

    I wrote my own Configuration library that supports variable expansion in properties files, have a look and see if it provides what you need. An article I wrote to introduce the feature is here.

    0 讨论(0)
  • 2020-12-17 02:51

    There is no direct way to substitute the property value within the property file/object but you may replace property value once you read via getProperty() method. To produce concatenated messages - have a look at MessageFormat class.

    String baseValue=prop.getProperty("url.main");
    
    0 讨论(0)
  • 2020-12-17 02:51

    I've done something like this and it isn't that difficult, you simply subclass the Properties class and implement your own getProperty method, check for a pattern and replace it if necessary.

    //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;
     }
    
    0 讨论(0)
  • 2020-12-17 02:55

    Apache Commons Configuration provides for this: http://commons.apache.org/configuration/

    Simple example code for loading the configuration file:

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

    You can have 'variable interpolated' properties, as described here 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}
    

    And it also allows you to include other configuration files while you're at it:

    include = colors.properties
    include = sizes.properties
    

    Besides a whole range of other features.

    0 讨论(0)
提交回复
热议问题