View on GitHub

Typed-settings

Typed Settings for your application

Download this project as a .zip file Download this project as a tar.gz file

typed-settings

Typed Settings for your application

Suppose you have an configuration.properties in your application where you define your application settings.

This configuration.properties can contains the following entries:

config.settings.port=8081
config.settings.host=localhost

standalone use

We start defining an interface with the methods to access the properties in the configuration.properties file. Each method needs to be annotated with @javax.annotation.Named annotation, and specify a key of the properties file to access as value.

public interface AppSettings {
    @javax.annotation.Named("config.settings.host")
    String getHost();
    @javax.annotation.Named("config.settings.port")
    Integer getPort();
}

Once we define our interface, we can create a SettingsBuilder object and specify the sources from where we will take the properties.

Optionally we can register some "customs" converters. typed-settings defines some basics converters but you can add your own, just implementing the interface Converter<T> and calling withConverter(...) method in the builder.

final SettingsBuilder builder = SettingsBuilder.create();
builder.addConfiguration(new PropertiesConfiguration(...));
builder.withConverter(Converters.dateConverter("dd.MM.yyyy"));

Finally we have to build the Settings object. This object, is basically a factory of proxies. Each call to getSettings(...) method, will create a proxy object that implements the interface that we provide as method argument.

final Settings settings = builder.build();
final AppSettings applicationSettings = settings.getSettings(AppSettings.class);

final String host = applicationSettings.getHost();
final Integer port = applicationSettings.getPort();

guice integration