Annotation based simpl configuration helper libbrary
It support properties and yaml file as config source file.
When using maven, edit pom.xml like this:
<dependency>
<groupId>com.github.hayarobi</groupId>
<artifactId>simple-config</artifactId>
<version>0.6</version>
</dependency>
<!-- This is optional only for reading yaml -->
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
package com.github.hayarobi.simple_config.sample;
import com.github.hayarobi.simple_config.annotation.Config;
@Config
public class DataConfig {
private String url;
private String user;
public String getUrl() {
return url;
}
public String getUser() {
return user;
}
}
sampleconf.properties
com.github.hayarobi.simple_config.sample.DataConfig.url=http://www.score.co.kr
com.github.hayarobi.simple_config.sample.DataConfig.user=tester
// ... code snippet
ConfigService confService = new ConfigServiceFactory().craeteServiceFromResource("sampleconf.properties");
// ...
// ... code snippet
DataConfig dataConf = confService.getConfig(DataConfig.class);
setConnInfo(dataConf.getUrl(), dataConf.getUser());
// ...
You can preload config objects. You can pass preloaded package names with comma separated string. You should add dependency on reflections package. pom.xml
<!-- only needed when preload enabled -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
</dependency>
// ... code snippet
ConfigService confService = new ConfigServiceFactory().createServiceFromResource("sampleconf.properties", true, "com.github.hayarobi.exmaple.conf,com.others.conf");
// ...
You can change and shorten config name instead of FQDN of class.
@Config("conf.data")
public class DataConfig {
private String url;
// ...
in properties file
conf.data.url=http://www.score.co.kr
conf.data.user=tester
Changing behaviors of config fields is possible by using annotation: @Name, @Required, @CaseSensitive and @Ignored
// ...
@Config
class SampleConfig
@Name("fruits")
private ArrayList<String> fruitList;
// enum literal should be case sensitive by default
@Required
@CaseSensitive(false)
private EnumSample planet;
// ...
You can set enum types just like string. and can change case sensitivity of enum fields by using @CaseSensitive annotation. (sensitive is default)
It's possbie, but generic class is not supported.
// ...
@Config("nested")
class NestedPOJOConfig {
private SubCategory1 cat1;
private SubCategory2 cat1;
private List<SubElement> subList;
// ...
}
Collection is supported. It can be the concrete class that has default public constructor, or well known abstract class or interface such as List, Set or SortedSet. These abstract classes will be set to concrete classes such as ArrayList, HashSet, TreeSet. The class of element can be any class that simple-config supports. It's possible to be like 'List<Map<String, HashSet>>', but not recommended.
yaml
list.sample:
people:
- name=John Doe
age=19
- name=James Kook
age=48
- name=Mary Sue
age=17
properties
list.sample.people.1.name=John Doe
list.sample.people.1.age=19
list.sample.people.9.name=Mary Sue
list.sample.people.9.age=17
list.sample.people.10.name=James Kook
list.sample.people.10.age=48
Map is also supported. Its characteristic is similar to that of collections. It also possible for well known abstract Map classes.
map.sample.cpu.core=intel
map.sample.cpu.zen=amd
map.sample.cpu.exynos=samsung
map.sample.cpu.snapdragon=qualcomm
- all primitive types and wrapping classes of them.
- String
- java.util.Date
- enum types
- Pojo (not containing generic)
- Class which implements Collection interface
- Map, SortedMap or other concrete implementation of Map having default public constructor.