Config Advanced
This page covers format selection, migrations, adapters, and runtime-aware reload patterns.
Format Selection
If your @ConfigFile uses {ext}, MagicUtils can switch formats using:
<config>.formatnext to the config filemagicutils.formatin the config root directory-Dmagicutils.config.format=...MAGICUTILS_CONFIG_FORMAT
Supported values include json, jsonc, yml, yaml, and toml depending
on the installed format helpers.
If multiple candidate files exist, MagicUtils picks one and logs a warning.
Format Migration
When the selected format changes and an older format file exists, MagicUtils can migrate the data into the new target file. This is useful when moving from JSONC to YAML or TOML without forcing users to recreate their configs.
Schema Migrations
Register ordered ConfigMigration steps to evolve config schemas:
MagicUtils stores the current schema version in config-version.
Custom Adapters
Use ConfigAdapters.register(...) for custom value types:
Register adapters before the affected config class is first loaded.
Change Subscriptions
Use subscribeChanges(...) when you want an unsubscribe handle:
Use onChange(...) when you only need fire-and-forget registration.
Runtime Resource Binding
MagicRuntime can rebuild managed resources on matching config changes:
Useful properties of this pattern:
- the latest resource is accessible via
binding.require() - the same resource is exposed through
runtime.requireNamedComponent("service.client", ReloadableClient.class) - replaced resources are closed automatically
binding.close()removes the named runtime component and stops listening for config changes
Validation And Constraints
@MinValue / @MaxValue
Clamp numeric config values to a safe range on load:
Out-of-range values are clamped to the range on load (a warning is logged). Set
warn = false on the annotation to suppress that log message.
@SaveTo
Store a field in a separate file instead of the main config document. The field
is excluded from the main file, written to (and loaded back from) the path in
the annotation, and multiple fields sharing one @SaveTo path are collected into
a single document keyed by each field's config path:
Use {ext} so the side file follows the active config format.
@ConfigSerializable
Enable a class for use inside config lists or maps:
Annotation Options
Several annotations carry options that are easy to miss:
| Annotation | Option | Default | Effect |
|---|---|---|---|
@ConfigValue | required | false | Fail loading if the key is absent instead of using the field default. |
@Comment | above | true | Place the comment above the key; false puts it inline. |
@ConfigFile | autoCreate | true | Write the file with defaults on first load when missing. |
@ConfigFile | template | "" | Seed a new file from a bundled template resource. |
@ConfigReloadable | notifyOnChange | true | Fire change listeners when a reloadable section changes. |
@ListProcessor
Apply per-item validation when loading lists:
The processor implements ListItemProcessor<T> and returns ProcessResult.ok(),
ProcessResult.modified(value), or ProcessResult.replaceWithDefault().
Hot Reload
Mark reloadable sections and listen for updates:
Use section-aware reloads to avoid rebuilding unrelated services: