Java 关于 application.properties 文件和环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2263929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regarding application.properties file and environment variable
提问by Arav
Java successfully recognizes the path in my application.properties file when I have the path configured as below:
当我将路径配置如下时,Java 成功识别了我的 application.properties 文件中的路径:
pathToInputFile=/kcs/data/incoming/ready/
pathToInputFileProcess=/kcs/data/incoming/work/
If I try using an environment variable, the Java program doesn't recognize the path.
如果我尝试使用环境变量,Java 程序将无法识别路径。
(the environmental variable TOM_DATA
is set as /kcs.)
(环境变量TOM_DATA
设置为 /kcs。)
pathToInputFile=${TOM_DATA}/data/incoming/ready/
pathToInputFileProcess=${TOM_DATA}/data/incoming/work/
Can I use an environment variable inside application.properties file?
我可以在 application.properties 文件中使用环境变量吗?
采纳答案by Tom Duckering
You can put environment variables in your properties file, but Java will not automatically recognise them as environment variables and therefore will not resolve them.
您可以将环境变量放在属性文件中,但 Java 不会自动将它们识别为环境变量,因此不会解析它们。
In order to do this youwill have to parse the values and resolve any environment variables you find.
为此,您必须解析这些值并解析您找到的任何环境变量。
You can get at environment variables from Java using various methods. For example: Map<String, String> env = System.getenv();
您可以使用各种方法从 Java 获取环境变量。例如:Map<String, String> env = System.getenv();
There's a basic tutorial here: http://java.sun.com/docs/books/tutorial/essential/environment/env.html
这里有一个基本教程:http: //java.sun.com/docs/books/tutorial/essential/environment/env.html
Hope that's of some help.
希望这会有所帮助。
回答by objects
Have a look at Commons configuration
Or alternatively use relative paths in your properties file, and load the base directory via command line as a system property. That way the property files remain independent of where the application is actually deployed.
或者在您的属性文件中使用相对路径,并通过命令行加载基本目录作为系统属性。这样,属性文件保持独立于应用程序实际部署的位置。
回答by Tim Lewis
Tom Duckering's answer is correct. Java doesn't handle this for you.
Tom Duckering 的回答是正确的。Java 不会为您处理此问题。
Here's some code utilizing regular expressions that I wrote to handle environment variable substitution:
这是我编写的一些使用正则表达式来处理环境变量替换的代码:
/*
* Returns input string with environment variable references expanded, e.g. $SOME_VAR or ${SOME_VAR}
*/
private String resolveEnvVars(String input)
{
if (null == input)
{
return null;
}
// match ${ENV_VAR_NAME} or $ENV_VAR_NAME
Pattern p = Pattern.compile("\$\{(\w+)\}|\$(\w+)");
Matcher m = p.matcher(input); // get a matcher object
StringBuffer sb = new StringBuffer();
while(m.find()){
String envVarName = null == m.group(1) ? m.group(2) : m.group(1);
String envVarValue = System.getenv(envVarName);
m.appendReplacement(sb, null == envVarValue ? "" : envVarValue);
}
m.appendTail(sb);
return sb.toString();
}
回答by user2514836
That is right. Java does not handle substituting the value of the environment variables. Also Java might not recognise variables like $EXT_DIR. While using such variables you might encounter FileNotFoundException. But Java will recognise the variables that are defined with -Din catalina.sh. What I mean by this, is suppose you have such a definition in catalina.sh
没错。Java 不处理替换环境变量的值。此外,Java 可能无法识别$EXT_DIR 之类的变量。使用此类变量时,您可能会遇到FileNotFoundException。但是 Java 会识别在catalina.sh中用-D定义的变量。我的意思是,假设您在 catalina.sh 中有这样的定义
CATALINA_OPTS="-Dweb.external.dir="$EXT_DIR"
In your properties file use ${web.external.dir}instead of using *$EXT_DIR*. And while accessing this property in your code you could do it this way:
在您的属性文件中使用${web.external.dir}而不是使用*$EXT_DIR*。在您的代码中访问此属性时,您可以这样做:
String webExtDir = System.getProperty("web.external.dir");
Hope this will help a lot of people so they won't have to pick bits and pieces from everywhere which takes really long to resolve an issue at hand.
希望这会帮助很多人,这样他们就不必从任何地方挑选零碎的东西,这需要很长时间才能解决手头的问题。
回答by kc2001
The Apache Commons project has expanded handling of properties files that lets you use environment variables(in particular, see the Variable Interpolation section). Then you should be able to get what you want using:
Apache Commons 项目扩展了对属性文件的处理,允许您使用环境变量(特别是,请参阅变量插值部分)。然后你应该能够得到你想要的使用:
pathToInputFile=${env:TOM_DATA}/data/incoming/ready/