user6618770
user6618770

Reputation: 77

How to store configuration in constants easy?

For some time I was working on the project, where all configuration was stored in constants of the Config class. I found this very comfortable: to use any property all that I need to write is Config.PROPERTY_NAME. Fast, simple, understandable. Much more comfortable than CONFIG.getString("StringNameWithoutAutocompletion", defaultValue).

But where it comes to add new configuration constant, there is some troubles: I need to copy-paste and then change existing constant in the Config class (twice in my project), and then try not forget add it in the .properties file. Also, if later I want to change property name, I need specify it in both places.

So, there is a question: it is possible to get clean Config.NAME properties without changing the Config class manually every time?

One of possible (but not really good for me) solutions that I can imagine is by using .java files instead of .properties, and compile them on program load.

I think the easiest way is to generate constants inside Config class automatically from the properties file, so if I add MyProperty in the .properties file, it will instantly become available as MY_PROPERTY constant. May be there is some library with power similar to Project Lombok?

Upvotes: 0

Views: 287

Answers (1)

Aaron
Aaron

Reputation: 881

If you want a constant defined on a class (Config in this case) you have to edit the class to add it.

If you really don't want to repeat yourself you could generate the constants class from the properties file, but that does mean you would have to regenerate the class every time you edited the properties file. Pick your poison.

Upvotes: 1

Related Questions