juneau-config
| Maven | |
|---|---|
| Jar | juneau-config-10.0.0.jar |
| OSGi | org.apache.juneau.config_10.0.0.jar |
| Links | Maven Central · Javadocs · Source |
Contents/Features
The juneau-config library contains a powerful API for creating and using INI-style config files.
See the guide: Externalize config with INI/YAML and variables
Key Capabilities:
- Support for storing and retrieving any of the following data types:
- Primitives
- POJOs
- Beans
- Arrays,
Maps, andCollectionsof anything - Binary data
- Transactional modifications with commit/rollback capabilities
- A listener API
- Filesystem watcher integration allowing changes on the file system to be reflected in real-time
- Modifications through the
Configclass (e.g. add/remove/modify sections and keys, add/remove comments and whitespace, etc...) DO NOT cause loss of formatting in the file - all existing whitespace and comments are preserved - Value encoding for added security
- Support for SVL variables
- Directly populate beans from config sections
- Accessing config sections through Java interface proxies
- An extensible storage API allows you to write your own config storage (e.g. storage in databases or the cloud)
Overview
# A set of entries
[Section1]
# An integer
key1 = 1
# A boolean
key2 = true
# An array
key3 = 1,2,3
# A POJO
key4 = http://bar
Config files are accessed through the Config class which are created through the Config.Builder class.
The config language may look simple but it is a very powerful feature with many capabilities.
See Config Overview for a walk-through of creating a Config and reading values.
Serializing configs
The following methods are used for serializing Config objects back into their text form:
Both methods are thread safe.
Writing to a Writer
writeTo(Writer) appends the current contents of the config to the specified writer and returns that same writer.
The writer is not closed by this method.
Config config = Config.create("MyConfig.cfg").build();
// Dump the config to a string.
StringWriter sw = new StringWriter();
config.writeTo(sw);
String contents = sw.toString();
// Or write it straight to a file.
try (Writer w = new FileWriter("MyConfig-copy.cfg")) {
config.writeTo(w);
}
toString() returns the same text that writeTo(Writer) produces.
The output reflects the in-memory state of the config, including changes that have been made but not yet committed (see Config Stores for where committed changes go).
Output format
Serialization goes through the config's ConfigFormat.
The format is auto-detected from the config name (a name ending in .yml or .yaml selects YAML, anything else selects INI),
or set explicitly with Config.Builder.ini(), Config.Builder.yaml() or Config.Builder.format(ConfigFormat).
See INI Config Format and YAML Config Files.
// A config named with a .yaml extension serializes as YAML.
Config yamlConfig = Config.create("MyConfig.yaml").build();
String yamlText = yamlConfig.toString();
// Force INI output regardless of the name.
Config iniConfig = Config.create("MyConfig.txt").ini().build();
String iniText = iniConfig.toString();
See also
- Microservice Config — how a microservice picks and watches its config file.
In This Section
🗃️ 2.5.1. Overview
1 item
🗃️ 2.5.2. Reading Entries
1 item
🗃️ 2.5.3. Variables
2 items
📄️ 2.5.4. Modded/Encoded Entries
The following method can be used to associates entry modifiers to a config:
📄️ 2.5.5. Sections
Config sections can be retrieved in-bulk using the Config.getSection(String) method.
🗃️ 2.5.6. Setting Values
1 item
📄️ 2.5.7. Listeners
Configuration change events can be listened for with Config.addListener(ConfigEventListener).
📄️ 2.5.8. Imports
Configurations can import values from other configurations using the following syntax:
🗃️ 2.5.9. Config Stores
1 item
📄️ 2.5.10. Read-only Configs
The following settings can be used to create read-only Config objects:
📄️ 2.5.11. Closing Configs
In general, it's good practice to close Config if you're only creating them temporarily so that their listeners get
📄️ 2.5.12. System Default Config
Each JVM has a system default config.
📄️ 2.5.13. YAML Config Files
juneau-config supports YAML config files as an alternative to INI/CFG files.
📄️ 2.5.14. Config Profiles & Relaxed Binding
Starting in 10.0.0, Juneau's config system supports two cloud-native ergonomics borrowed from the Spring Boot
📄️ 2.5.15. INI Config Format
A config file's on-disk syntax is handled by a ConfigFormat.