开发者

Is there default properties file to put configurations into?

开发者 https://www.devze.com 2023-02-24 08:00 出处:网络
Any default propertie开发者_运维问答s file which java can automatcially load?The short answer is no.

Any default propertie开发者_运维问答s file which java can automatcially load?


The short answer is no.

The somewhat longer answer starts with a question itself: What should be configured by this file?

For the logging-API of java (java.util.logging) exists a standard-properties-file to configure it. Other frameworks may as well use standard-configuration files. But that always configure only stuff specific for this framework.

If you want to have persistent configuration, you probably want to use the Preference-API. That allows you to save configuration-data, that stays with the user or the JVM.


You can use your own default properties file. You can do it with just few lines.

There is also built in properties, however, these are no simpler, but they are standard.

IMHO Most of the time a plain Proeprties file is used.

This example from the tutorial. This is a more complex example, you can have just one properties file.

// create and load default properties
Properties defaultProps = new Properties();
FileInputStream in = new FileInputStream("defaultProperties");
defaultProps.load(in);
in.close();

// create application properties with default
Properties applicationProps = new Properties(defaultProps);

// now load properties from last invocation
in = new FileInputStream("appProperties");
applicationProps.load(in);
in.close();


You have to load your properties file yourself or pass arguments at command line.


There is a small library that you can use which will load properties automatically for you: Confucius.

0

精彩评论

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

关注公众号