https://github.com/brianm/config-magic
Convenience Configuration Library for Java
https://github.com/brianm/config-magic
Last synced: 6 months ago
JSON representation
Convenience Configuration Library for Java
- Host: GitHub
- URL: https://github.com/brianm/config-magic
- Owner: brianm
- Created: 2009-10-07T16:23:05.000Z (almost 17 years ago)
- Default Branch: master
- Last Pushed: 2024-03-23T18:28:55.000Z (over 2 years ago)
- Last Synced: 2025-07-20T05:47:26.455Z (12 months ago)
- Language: Java
- Homepage:
- Size: 179 KB
- Stars: 81
- Watchers: 9
- Forks: 28
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Example
Create an interface for your config object:
public interface MyConfig
{
@Config("foo")
String getFoo();
@Config("blah")
int getBlah();
@Config("what")
@Default("none")
String getWhat();
}
Set the properties that we mapped with `@Config` above (or simply call `System.getProperties()`):
Properties props = new Properties();
props.setProperty("foo", "hello");
props.setProperty("blah", "123");
Then create the config object from the properties:
ConfigurationObjectFactory factory = new ConfigurationObjectFactory(props);
MyConfig conf = factory.build(MyConfig.class);
# Default values
Using `@Default()` can set arbitrary default values. To set `null` as the default value, use the `@DefaultNull`annotation.
# Advanced usage
@Config({"what1", "what2"})
@Default("none")
String getWhat();
will look at `what1` first, then at `what2` and finally fall back to the default.
# Parameterized configs
enum StateType {
FILESYSTEM,
MEMORY
}
@Config("${state_type}.size")
@DefaultNull
String getParameterizedConfig(@Param("state_type") StateType e);
@Config("${state_type}.${source}.path")
@DefaultNull
String getDoubleParameterizedConfig(@Param("state_type") StateType e, @Param("source") String source);
Use it like the following:
myConfig.getParameterizedConfig(StateType.MEMORY); // reads from MEMORY.size
myConfig.getDoubleParameterizedConfig(StateType.FILESYSTEM, "backend"); // reads from FILESYSTEM.backend.path
# Type support
Config-magic supports these types:
* Primitive types: `boolean`, `byte`, `short`, `integer`, `long`, `float`, `double`.
* Enums. Note that config-magic by default ignores the case for enum values.
* `java.lang.String`.
* `java.net.URI`.
* `java.lang.Class` and simple wildcard extensions (`java.lang.Class>`, `java.lang.Class extends Foo>` - config-magic will type check that the type passed as a property conforms to the wildcard type), but not more complex wildcard or parameterized types (e.g. `java.lang.Class super Bar>` or `java.lang.Class extends List super Bar>>`).
* `org.skipe.config.TimeSpan`: constructed from short textual representation like "5d" (or alias "5 days"); units supported are:
* ms (alias 'milliseconds')
* s ('seconds')
* m ('minutes')
* h ('hours')
* d ('days')
* Any instantiable class that has a public constructor with a single `Object` parameter. This is useful for instance for [joda-time](http://joda-time.sourceforge.net/)'s `DateTime` objects.
* Any instantiable class that has a public constructor with a single `String` parameter. This is useful for instance for `java.lang.File`.
* Any class that has a static `valueOf` method with a single `String` parameter and the class as its return type.
# Maven dependency
To use config-magic in Maven projects:
org.skife.config
config-magic
0.11
# Mailing List
We have a [mailing list](http://groups.google.com/group/config-magic) for development and users.