I am working on a product suite which has 4 products. Right now, all of the configuration data is either in the XML or properties files.This approach is not maintainable as we have to manage different configuration file for different environment(for e.g. Production, Development etc).
So, what is the best way to handle configuration data?
Also, can we modularize this into a separate module? So that all products can use this module. We don't want to use the property files. I am looking for a solution in which we can move all of the configuration specific code as a new configuration module and persist all the configuration data in database.
Configuration management is a systems engineering process for establishing consistency of a product's attributes throughout its life. In the technology world, configuration management is an IT management process that tracks individual configuration items of an IT system.
Configuration management (CM) is a systems engineering process for establishing and maintaining consistency of a product's performance, functional, and physical attributes with its requirements, design, and operational information throughout its life.
We find it a better way do do config than a file because it means you can easily programmatically change config values through an admin interface when needed, which can enforce logic around what can go into each setting. You can't do that so easily with a file (though, of course, it is possible).
Using commons-configuration you have a unified API for accessing the properties, no matter how they are represented - .properties, xml, JNDI, etc. For example:
config.properties
:
jdbcHost=192.168.12.35
jdbcUsername=dbuser
jdbcPassword=pass
config.xml
:
<config>
<jdbcHost>192.168.12.35</jdbcHost>
<jdbcUsername>dbuser</jdbcUsername>
<jdbcPassword>pass</jdbcPassword>
</config>
in both cases they will be accessible with something like:
String host = config.getString("jdbcHost");
You're almost there... I would keep your same approach and pull in the correct configuration file for the instance of the application that is running by a method similar to either of the following:
Name all of your configuration files differently and have your application pull them in by some unique criteria (username, hostname, etc.):
Keep them outside the codebase in a location based on an environment variable that the application assumes exists:
I've even used a combination of these approaches on the same project (#1 for build process configurations and #2 for runtime configurations).
If your applications work with a database, you can create a "configuration" table as follows:
create table configuration (mode char(3), key varchar(255), value varchar(1023));
You would initialize it using an init script, say init.sql with contents along the lines of:
insert into configuration values ('pro', 'param1', 'value1'); -- production
insert into configuration values ('dev', 'param1', 'value1'); -- development
insert into configuration values ('tst', 'param1', 'value1'); -- testing
...
The benefits of this approach are as follows:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With