Sony
Sony

Reputation: 351

java properties files, using a key as another keys value, is this possible

I would like to know if there is any way to use a key as another key's value in .properties files.

For example is this possible, or is there any other way to achieve something similar to this?

key = another_key

app.name=Stack Over Flow  
app.title.welcome=Welcome to {app.name} 

so when i get the value of app.title.welcome it should be "Welcome to Stack Over Flow"

Upvotes: 4

Views: 4695

Answers (4)

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78579

The Apache Commons Configuration Project has an implementation capable of doing variable interpolation.

Read the section named Variable Interpolation

application.name = Killer App
application.version = 1.6.2
application.title = ${application.name} ${application.version}

You would need this third-party library in your class path, but on the other hand you will not have to worry about writing yet another implementation for this :)

You might like to read the Properties How To as well.

Upvotes: 7

WhiteFang34
WhiteFang34

Reputation: 72039

You might want to check out Apache Commons Configuration. It does what you're looking for and more.

Upvotes: 2

bvk256
bvk256

Reputation: 1903

You can just write app.title.welcome=Welcome to {0} and pass app.name as a parameter in Java code.

Upvotes: 0

Bozho
Bozho

Reputation: 597046

With java.util.Properties - no. But if you write the proper resolver - it is. And writing that resolver won't be that hard. Just look for {..} in each value and whenever encountered look for that key.

Upvotes: 1

Related Questions