user710818
user710818

Reputation: 24248

Java, Spring best practices to use variables in properties files

I use org.springframework.beans.factory.config.PropertyPlaceholderConfigurer. There are 3 properties files. So if properties duplicate - last wins.

The first file contains all properties, others substitute some properties. I use e.g. variable like

log.dir=c:/log.

So I can substitute part of path in properties like

${log.dir}/app1.log

In first file I have 48 uses of this variable.

In second file I have 25 substitutes for properties from first file, and 5 substitutes in third file.

The problem that I need redefine this variable also in second and third file!

I would like use the same name

log.dir

But if I define it in second or third file - redefining go back to first file - but I need use for the rest 18 (48 -25 - 5=18) old properties. What the best practices in similar cases? Thanks. After some analysis I would like have variable that has the same name in all properties files, but values must not be overridden. So for first file log.dir=c:\log, for the second log.dir=d:\log, for the third log.dir=e:\log. And when calculating paths for each file used own. So overridden must be only properties - not variables.

Upvotes: 1

Views: 2444

Answers (1)

MaDa
MaDa

Reputation: 10762

What (I think) you'd like to do is to make the log.dir variable changing the value as the configurer steps through the property files and encounters your overriding definitions.

The problem is, the PropertyPlaceholderConfigurer does not work this way; for every encountered ${placeholder} definition it checks only the winning redefinition. It does resolve placeholders taking into account the context of the file they're defined in.

I see only one feasible solution: use a different variable name in each file. Writing your own placeholder configurator is probably very poor gain-to-workload ratio.

Upvotes: 2

Related Questions