Re: Configuration -- XML or Properties file?
Monday, September 12, 2005 08:47 AM
As with most things in software, "it depends". Is this configuration information going to be loaded upon your application startup or will this be wrapped within a Singleton and loaded only once (but loaded the first time initialized)? In my experience I have seen both of these methods of configuration used practice as well as loading from a database table. Here are my opinions of the three choices:
1. Properties file: This is a quick/easy way to handle configuration. It lacks the ability for specifying anything but the most basic information (name=value pairs) and you usually must specify more parameters than usual in this manner due to potential clashes. For example:
Additionally, you must handle the name values in your code using String objects. This is very inflexible. Another important thing the keep in mind here is that runtime configuration changes would require knowing the location of the configuration file and modifying it and then rewriting it back. (JMX could possibly used to handle this aspect.)
2. Database Table: Allows for more complex storage method and for handling multiple sets of information through the use of primary keys into a value table(s). In other words, you can circumvent the above problem in #1 simple by having a primary key for each type of datasource and then index this into the value table by the key. Drawbacks are that you now have to open a database connection (use pooling) to read the values. A benefit is that it is much easier to allow for runtime changing of the configuration.
3. XML file: While it is also file-based (as in #1), it allows for handling multiple sets of the same type of data in a much more manageable manner. You can use a DTD to specify the parsing of the information and map this to a Java object (if you choose). The drawbacks of runtime changes are the same as #1 above.
I know there are other positives/negatives to each that I haven't mentiond, however in the end I think it's up to the architect of the application to apply the proper configuration approach based on things such as experience with each method, existing application within the organization, requirements of needing runtime changes without a server restart, etc.
I personally tend to use #2 and #3 most often with my work, however if I'm developing a simple app. and only need to handle configuration of a few pieces of data, I'll simply use a properties file.