如何在用户定义的.properties文件中引用系统属性? [英] How to reference a system property within a user-defined .properties file?

查看:698
本文介绍了如何在用户定义的.properties文件中引用系统属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的生产.properties文件定义一个工作目录的属性(比如 work.dir = / home / username / working-directory ),没有硬编码 / home / username

I want to define a property for a working directory(say work.dir=/home/username/working-directory), for my production .properties file, without hard-coding the /home/username.

我想引用系统属性 user.home 在硬编码的 / home / username 上,使 work.dir 更通用。

I want to reference the system property user.home in place on the hard-coded /home/username, to make work.dir more generic.

如何引用系统属性并将其连接到用户定义的.properties中的其他用户定义字符串?

How do I reference the system property and concatenate it will other user-defined strings in a user-defined .properties?

注意:我不想在我的java代码中访问user.home属性,而是从我定义的.properties中访问。我希望能够用我的生产和开发的不同值替换 work.dir 的值(例如JUnit测试)。

Note: I don't want to access the user.home property in my java code, but from the .properties that I have defined. I want to be able to replace the value of the work.dir with different value for both my production and development(JUnit tests for example).

推荐答案

从文件中获取属性,然后替换支持的宏。

Get the property from the file, then replace supported macros.

String propertyValue = System.getProperty("work.dir");
String userHome = System.getProperty("user.home" );
String evaluatedPropertyValue = propertyValue.replace("$user.home", userHome );

这篇关于如何在用户定义的.properties文件中引用系统属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆