smartics-properties is a library for Java to declare and define properties and to access them from an application in a convenient way. Convenience means that properties are easily
Properties are used to configure an application. This is what the well-known Properties class of the Java SDK is all about: Simple key/value tuples of type String. It is quite easy to load property files from a stream or from the class path. But a standardized way to provide property values is not provided out-of-the-boy. In other words: The developer has to define a location to place the properties file and write the couple of lines of code to access it. Once in memory, the developer has to manage the properties and make it accessible by all clients.
What would be more convenient if the developer declares the properties at one place and provides the property values at another. Often these tasks are executed by different people anyway. And without further ado the system makes the property values available through a simple API. This is what smartics-properties can do for you. Let's have a quick walk through the steps of declaring, defining and accessing properties with this library.
Property Sets are declared as an interface like this:
@PropertySet("app") public interface Application { String name(); }
This example provides a simple String property. The property set defines the prefix of the key, the value is identical with the name of the method in the interface. While the PropertySet annotation is mandatory, the name of the set is not. If you leave the value for the annotation, the prefix defaults to the fully qualified class name of the property set interface. And if you do not want a prefix, simply provide the empty String as the property set name.
A property definition provides property values for declared properties. In this simple example we provide a properties file with the same name (including the package!) as the declaring class: Application.properties.
app.name=test
This example provides a String property with key app.name. The is the key you surely have expected.
After declaring and defining the property we want to access it through a configuration. A configuration is a set of property sets. There are a couple of implementations for configurations. In the following example we use ClasspathConfigurationProperties:
final ClasspathConfigurationPropertiesFactory factory = new ClasspathConfigurationPropertiesFactory(); final ClasspathConfigurationProperties config = factory.createManagement(); config.addClassPathProperties(Application.class);
Via a factory we create the configuration and explicitly instruct it to load the properties declaration with their definitions (that is the interface and the properties file).
If you do not want to do the loading stuff explicitly, you may choose an implementation that retrieves all properties from the classpath automatically.
final ConfigurationPropertiesFactory factory = ConfigurationPropertiesFactoryFactory.createDefaultFactory(); final ConfigurationPropertiesManagement config = factory.createDefaultManagement();
final Application app = config.getProperties(Application.class); final String name = app.name();
Using the configuration created in the previous step, the last two lines of this short introduction access the property set instance and then read the property value name.
What we have seen is not very spectacular. We declared a property set with one property, provided a definition via a properties file on the classpath and showed how we can access it through the client API.
What would make the use more interesting is to see, how we
Please refer to the tutorials to cover answers to these topics in depth!