本文介绍了关于 application.properties 文件和环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将路径配置如下时,Java 成功识别了我的 application.properties 文件中的路径:

Java successfully recognizes the path in my application.properties file when I have the path configured as below:

pathToInputFile=/kcs/data/incoming/ready/
pathToInputFileProcess=/kcs/data/incoming/work/

如果我尝试使用环境变量,Java 程序将无法识别路径.

If I try using an environment variable, the Java program doesn't recognize the path.

(环境变量TOM_DATA设置为/kcs.)

(the environmental variable TOM_DATA is set as /kcs.)

pathToInputFile=${TOM_DATA}/data/incoming/ready/
pathToInputFileProcess=${TOM_DATA}/data/incoming/work/

我可以在 application.properties 文件中使用环境变量吗?

Can I use an environment variable inside application.properties file?

推荐答案

您可以将环境变量放在属性文件中,但 Java 不会自动将它们识别为环境变量,因此不会解析它们.

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.

为了做到这一点,必须解析这些值并解析您找到的任何环境变量.

In order to do this you will have to parse the values and resolve any environment variables you find.

您可以使用各种方法从 Java 获取环境变量.例如:Mapenv = System.getenv();

You can get at environment variables from Java using various methods. For example: Map<String, String> env = System.getenv();

这里有一个基本教程:http://java.sun.com/docs/books/tutorial/essential/environment/env.html

希望对您有所帮助.

这篇关于关于 application.properties 文件和环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 14:49