|
This tool allows configuration files (such as .properties and .xml) to be bound dynamically and painlessly into a Java interface. This has the benefit of cutting significantly on the boiler plate code of reading property files for every project. In addition, it supports serializing String properties as well as parameterized List properties. This is accomplished through meta-programming. In a nutshell, you create simple Java interface and use annotations to specify what the configuration file will be and how the properties will be interpreted. Here is a simple example: Property FilesMainConfiguration.java@Configurable(name="app.properties")
public interface MainConfiguration {
@Property(key="prop1_key")
String getValue2ListAsString();
@Property(key="prop2_key")
List getValue2List();
}In this example above, I created an interface called MainConfiguration. Using this interface, I will bind at runtime properties from file app.properties. What this tool will do is create a CGLIB dynamic proxy against this interface and associate the values of the properties matching those keys to the return types of the methods. app.propertiesprop1_key = value1
prop2_key = value3, value4, value5
XML FileIn addition, the library supports a basic level of XPath in order to uniquely determine a property key. As in the following example: MainXMLConfiguration.java@Configurable(name="test.xml", strategy=StrategyType.XML)
public interface IXMLConfigurableStub {
@Property(key="/root/key1")
String getValue1();
@Property(key="/root/key2list")
List<String> getValue2List();
@Property(key="/root/key4/intList")
List<Integer> getValue4List();
}The property XML file in this case will be: app.xml<root>
<key1>value1</key1>
<key2list>value2, value3</key2list>
<key4>
<intList>1,2,3,4</intList>
</key4>
</root>
|