Using properties in a properties file

I apologize for the title. I could not find a better way to explain the situation.

I use to load a properties file using the property class, as described in the URL http://www.exampledepot.com/egs/java.util/Props.html

My question is: can I use properties inside this properties file?

Example:

test.properties

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

maybe with a different syntax?

thank

+6
java properties
Jan 15 2018-12-12T00:
source share
5 answers

Never seen this before. Of course, you could make your own preprocessor. As long as the reference property appears 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 allow duplication by specifying various properties:

  • change: url.games={url.main}/games in url.games_extension=/games
  • prepend: url.main to url.games_extension to get the full game url in your application code.
+3
Jan 15 '12 at 12:15
source share
β€” -

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

Simple sample code to download the configuration file:

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

You may have variable interpolation 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 are on it:

 include = colors.properties include = sizes.properties 

In addition to a number of other functions.

+5
Jan 15 '12 at 12:22
source share

I wrote my own configuration library, which supports the extension of variables in property files, to look and see if it provides what you need. In the article I wrote to introduce this feature, here .

+2
Feb 09 '13 at 16:37
source share

There is no direct way to replace the property value inside the file / property object, but you can replace the property value after you read it using the getProperty() method. To create concatenated messages, view the MessageFormat class.

 String baseValue=prop.getProperty("url.main"); 
+1
Jan 15 '12 at 12:11
source share

I did something like this and it’s not that difficult, you just subclass the Properties class and implement your own getProperty method, check the template 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 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; } 
+1
Jan 15 2018-12-15T00:
source share



All Articles