开发者

Java, Spring best practices to use variables in properties files

开发者 https://www.devze.com 2023-04-09 08:29 出处:网络
I use org.springframework.beans.factory.config.PropertyPlaceholderConfigurer. There are 3 properties files. So if properties duplicate - last wins.

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.

T开发者_如何学Gohe 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号