TryHard
TryHard

Reputation: 379

Best practice for storing config data

What is the best procedure for storing configuration data?
I have several classes that require some configuration data that is only needed in their class.
Should I load all this data from a configuration file or should I hardcode it into the classes?
Many thanks in advance

Upvotes: 2

Views: 1120

Answers (1)

Mark Bramnik
Mark Bramnik

Reputation: 42541

To address your question:

Should I load all this data from a configuration file or should I hardcode it into the classes?

Basically if you hard code some value into the class, you don't intend to change that value in different environments. Each such a change would require re-compilation of the project.

For example if you have a constant for PI=3.14 it doesn't make sense to use different values for different environments.

Alternatively if you go with configuration files, the update of such a file that can be supplied with a deployment script is much easier.

An example of this can be a host/port of the database. Development might use one host, production might use another.

So you should decide what works for you best.

This is common for all types of applications (not only spring boot driven).

Now its true that in spring boot you can create a configuration file (properties or yaml) and place it into the artifact (by putting it into src/resources/ or src/resources/config).

For some situations its good enough, for others you might use another way of configuration.

I don't refer managing secrets here, this is a more advanced stuff, but in general you won't want to manage things like passwords neither in the source code (hard coded) nor in the configuration file.

Upvotes: 3

Related Questions