1

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

2 Answers 2

4

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")); 
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.