A strongly typed configuration framework with detailed validation that is simple to use, for web apps, command line or any application environment.
Strong Typing & Detailed validation & Simple to use & Fails Fast & Self Documenting Complete Usage Example
import yarnandtail.andhow.property.*;
import yarnandtail.andhow.*;
import yarnandtail.andhow.load.PropFileLoader;
public class SimpleSample {
public static void main(String[] args) {
AndHow.builder() /* 1) Simple builder initializes framework */
.loader(new PropFileLoader())
.group(MySetOfProps.class) /* 2) MySetOfProps defined below */
.build();
//3) After initialization, Properties can be used to directly access their values.
//Note the strongly typed return values.
String queryUrl =
MySetOfProps.SERVICE_URL.getValue() +
MySetOfProps.QUERY_ENDPOINT.getValue();
Integer timeout = MySetOfProps.TIMEOUT.getValue();
System.out.println("The query url is: " + queryUrl);
System.out.println("Timeout is : " + timeout);
}
//4) Normally PropertyGroups would be in separate files with the module they apply to
@GroupInfo(name="Example Property group", desc="One logical set of properties")
public interface MySetOfProps extends PropertyGroup {
StrProp SERVICE_URL = StrProp.builder().mustEndWith("/").build();
IntProp TIMEOUT = IntProp.builder().defaultValue(50).build();
StrProp QUERY_ENDPOINT = StrProp.builder().required()
.desc("Service name added to end of url for the queries").build();
}
}AndHowinitializes with a simple builder(). Loaders, like thePropFileLoader, read properties from various sources. PropFileLoader will look for an andhow.properties file by default, but other locations can be configured and other loaders can be used, such as command line and (soon) JNDI.- Add
PropertyGroups to the list that AndHow will manage. Propertyvalues can be accessed directly from the Property itself - there is no magic name string to fetch properties from a HashMap. Return values ofgetValue()are strongly typed.- Properties are defined as
public static finalconstants in interfaces that create logical sets calledPropertyGroups. Properties describe themselves in their builders, including the value type (String, Integer, etc), required vs. optional, validation rules and description. (Members defined in interfaces arepublic static finalby default)
Of course, this example fails because there is no andhow.properties file. This is a good thing - you would not want a misconfigured application to appear to start. Even better, it doesn't just fail, it prints a sample configuration for each of the loaders in use. For the PropFileLoader it would print this:
##########################################################################################
# Sample properties file generated by AndHow! strong.simple.valid.App_Configuration ####
################################################## https://github.com/eeverman/andhow ###
# Property Group 'Example Property group' - One logical set of properties
# Defined in interface org.simple.SimpleSample.MySetOfProps
# SERVICE_URL (String)
# The configured value must end with '/'
org.simple.SimpleSample.MySetOfProps.SERVICE_URL = [String]
# TIMEOUT (Integer)
# Default Value: 50
org.simple.SimpleSample.MySetOfProps.TIMEOUT = 50
# QUERY_ENDPOINT (String) REQUIRED - Service name added to end of url for the queries
org.simple.SimpleSample.MySetOfProps.QUERY_ENDPOINT = [String]
##########################################################################################Read more about project goals and details. Contact me if you have questions or would like to help.
&?!