java 什么是属性文件?它包含什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1366611/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:16:59  来源:igfitidea点击:

what is properties file? and what does it contain?

java

提问by i2ijeya

I want to know what is the use of .properties file and where it is used? Currently i am doing dynamic web application with ANT and i have to use .properties file and have to set a property inside build.xml file. So please gimme ideas regarding this

我想知道 .properties 文件的用途是什么,用在什么地方?目前我正在使用 ANT 做动态 Web 应用程序,我必须使用 .properties 文件并且必须在 build.xml 文件中设置一个属性。所以请给我一些关于这个的想法

Regards, Jeya

问候, 杰亚

回答by Michael Borgwardt

A .properties file is a simple collection of key-value pairs that can be parsed by the java.util.Propertiesclass.

.properties 文件是可由java.util.Properties类解析的键值对的简单集合。

Properties files are widely used for many purposes in all kinds of Java applications, often to store configuration or localization data.

属性文件在各种 Java 应用程序中被广泛用于多种用途,通常用于存储配置或本地化数据。

ANT propertiesare an ANT-specific concept and not identical to properties files, though ANT properties can be read from such a file, usually in order to have a central point to store frequently used and changing configuration data. Then you can have a line like this in different ANT scripts:

ANT 属性是一个特定于 ANT 的概念,与属性文件不同,尽管可以从这样的文件中读取 ANT 属性,通常是为了有一个中心点来存储经常使用和更改的配置数据。然后你可以在不同的 ANT 脚本中有这样一行:

<property file="foo.properties"/>

And all the scripts can then use those properties.

然后所有脚本都可以使用这些属性。

回答by i2ijeya

Properties are used to externalize the data which is configurable and if you put that data in your code you have to build the code each time you want to change the value of the property. The main advantage of properties is that they are outside your source code and you can change them anytime. To use properties refer to java.util.Propertiesclass.

属性用于外部化可配置的数据,如果您将该数据放入代码中,则每次要更改属性值时都必须构建代码。属性的主要优点是它们在您的源代码之外,您可以随时更改它们。要使用属性,请参考java.util.Properties类。

回答by Rob Spieldenner

Given your Ant file contains something like

鉴于您的 Ant 文件包含类似

<property file="build.properties"/>
<property name="prop1" value="value1"/>

if build.properties contains a line

如果 build.properties 包含一行

prop1=valueFromPropertyFile

Then all references to prop1 will give you valueFromPropertyFile.

然后所有对 prop1 的引用都会给你 valueFromPropertyFile。

Best practices would be for all properties to be set in build.xml and have build.properties existence be optional to override specific settings for different environments. These properties can also be overridden on the command line with:

最佳做法是在 build.xml 中设置所有属性,并且可以选择是否存在 build.properties 以覆盖不同环境的特定设置。这些属性也可以在命令行上覆盖:

ant -Dprop1=valueFromCommandLine

Command line takes precedence over build.properties which takes precedence over whatever is in build.xml.

命令行优先于 build.properties,后者优先于 build.xml 中的任何内容。

Specific examples in the web domain:

网域中的具体例子:

  • Different IP address to deploy to between production, test, and development.
  • Setting a flag to compile with debugging information off and on
  • Setting a flag on whether to deploy an exploded war/ear or the packaged version
  • Choosing whether to run smoke tests, unit tests, integration tests, acceptance tests or everything
  • Different passwords between environments
  • Whether to create and populate the database or use an existing one
  • Which web.xml to package depending if deploying to Tomcat, GlassFish, JBoss, etc.
  • 在生产、测试​​和开发之间部署到不同的 IP 地址。
  • 设置一个标志以关闭和打开调试信息进行编译
  • 设置是否部署爆炸War/耳朵或打包版本的标志
  • 选择是否运行冒烟测试、单元测试、集成测试、验收测试或其他所有测试
  • 环境之间的不同密码
  • 是创建和填充数据库还是使用现有数据库
  • 打包哪个 web.xml 取决于是否部署到 Tomcat、GlassFish、JBoss 等。

回答by Nick Holt

Properties files allow you to separate function (what the build.xml does) from the data it uses to perform that function (the properties).

属性文件允许您将功能(build.xml 的作用)与其用于执行该功能的数据(属性)分开。

For example, different developers on a project might have a different project root directories. The build would need to know which directory is the root, after which the functions it performs would be based on that directory. So rather than having a custom build.xml per developer, you would put this information in a properties file.

例如,一个项目的不同开发人员可能有不同的项目根目录。构建需要知道哪个目录是根目录,之后它执行的功能将基于该目录。因此,与其为每个开发人员定制一个 build.xml,不如将此信息放在一个属性文件中。