7.8. Using Composite Configuration
Problem
Your application calls for a multilayered configuration where a set of default properties can be selectively overridden by local or user configuration preferences.
Solution
Create a configuration.xml
file that contains
references to multiple properties files, and pass this file to a
ConfigurationFactory
. A
ConfigurationFactory
will then return a
Configuration
implementation that obtains
configuration parameters from multiple properties file.
Table 7-1 lists configuration properties for an application. A global configuration layer defines default values for configuration parameters. A local configuration layer allows you to customize the behavior of a system at a particular site, and the user configuration layer refines configuration parameters for a specific user. When an application retrieves the value of “name,” the user layer’s value of “Sean” overrides the global layer’s value of “Default User.”
Table 7-1. Three layers of configuration
Property |
Global |
Local |
User |
---|---|---|---|
threads.max |
50 |
30 | |
threads.min |
20 |
1 | |
timeout |
15.52 | ||
interactive |
TRUE | ||
color |
red |
black | |
speed |
50 |
55 |
75 |
name |
Default User |
Sean |
Properties are stored in three separate files shown in Examples Example 7-2 (global.properties
),
Example 7-3 (local.properties
),
and Example 7-4
(user.properties
).
Example 7-2. global. properties
threads.max=50 threads.min=2 timeout=15.52 interactive=true color=red speed=50 name=Default User
Example 7-3. local .properties
# Overrides ...
Get Jakarta Commons Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.