问题
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://www.exampledepot.com/egs/java.util/Props.html
my question is can I use properties within that properties file ?
example:
test.properties
url.main="http://mysite.com"
url.games={url.main}/games
url.images={url.main}/images
.
.
.
is that possible with some other syntax?
thanks
回答1:
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:
- change:
url.games={url.main}/games
intourl.games_extension=/games
- prepend:
url.main
tourl.games_extension
to get the full games url in your application code.
回答2:
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.
回答3:
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.
回答4:
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");
回答5:
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;
}
来源:https://stackoverflow.com/questions/8869482/using-properties-within-the-properties-file