Kevin
Kevin

Reputation: 6292

Best place to save Java configuration information

I am writing a library which uses some configurations. These configuration will not change frequently but will possible change in the future. Therefore I don't want to hard code them in the code.

What is the best way of doing this for a Java library? One possible solution is to inject it and hard code them in a DI configuration file I assume. But this requires code change as well.

What is the most typical way of doing this?

Many thanks

Upvotes: 2

Views: 289

Answers (2)

dash1e
dash1e

Reputation: 7807

You can do this:

  • Add the default configuration as an embedded resource file to the library jar
  • Then the user can override the file adding a resource with the same path to the classpath

As file format you can use the Java Properties File.

And you can load it in this way

Properties props = new Properties();
props.load(YourClass.class.getResourceAsStream("name-of-your-prop-file.properties"));

Upvotes: 4

Petr Janeček
Petr Janeček

Reputation: 38414

Java Util Properties is a great tool for configuration. You can have the file embed in the jar, if you want to, and load it using getResourceAsStream() method.

Upvotes: 1

Related Questions