Quickstart
Add depedendency
Constretto is available from the central Sonatype OSS repository, so it is easy to add it to your project
Maven
If you're using Maven, add the following to your pom:
<dependency> <groupId>org.constretto</groupId> <artifactId>constretto-core</artifactId> <version>2.2.3</version> </dependency>
SBT
libraryDependencies += "org.constretto" % "constretto-core" % "2.2.3"
Add @Configuration or/and @Configure to POJO
In order to make your POJO "Constrettofied", you use the @Configuration (for fields and parameters) or @Configure (on methods, including constructors)
public class DataSourceConfiguration { private String myUrl; private String myPassword; private Integer version; // When no expression is explicitly given Constretto will use field name as key @Configuration private String vendor; @Configuration("username") private String myUsername; @Configure public void configure(String url, @Configuration("password") String secret) { this.myUrl = url; this.myPassword = secret; } @Configure public void setVersion(Integer version) { this.version = version; } public String getUrl() { return myUrl; } public String getUsername() { return myUsername; } public String getPassword() { return myPassword; } public String getVendor() { return vendor; } public Integer getVersion() { return version; } }
Load configuration
Tell Constretto where to read configuration from by constructing a ConstrettoConfiguration object. Constretto-Core currently supports
Java properties files, Encrypted Java properties files, Windows-style INI files, JSON, POJOs and System properties (automatically added when using the
ConstrettoBuilder
.
... final ConstrettoConfiguration configuration = new ConstrettoBuilder(). // System properties are added as default .createPropertiesStore() // for standard Java property files .addResource(Resource.create("file:/etc/app/database.properties")) .done() // Multiple resources may be added .createEncryptedPropertiesStore(SHARED_SECRET) // for Java property files with encrypted values .addResource(Resource.create("http://companyserver/encrypted.properties")) .done() .createIniFileConfigurationStore() // For Windows-style INI files .addResource(Resource.create("classpath:configuration.ini")) .done() .createJsonConfigurationStore() // Configuration in JSON format .addResource(Resource.create("http://companyserver/sharedConfiguration.json")) .done() .createObjectConfigurationStore() // Configuration as POJOs .addObject(myPojo) .done(); .getConfiguration(); ...
Inject configuration
Use your ConstrettoConfiguration
object to inject configuration in your POJOs.
... DataSourceConfiguration dataSourceConfiguration = new DataSourceConfiguration(); configuration.on(dataSourceConfiguration); // for existing objects ... DataSourceConfiguration dbConfig = configuration.as(DataSourceConfiguration.class) // Constretto constructs the pojo and injects configuration ...
Environment tagging support
Constretto supports storing environment-specific configuration by using tags. When using the Java property file format you can specify it using the @(tag) prefix:
somedb.username=default username @production.somedb.username=username in production @systest.somedb.username=username in system test
Telling Constretto which tags to look up
Constretto uses a System property, or System environment property to know what tags to look up. this property is called "CONSTRETTO_TAGS"
Example:$java MyApp -DCONSTRETTO_TAGS=tag1,tag2,tag3Or
$export CONSTRETTO_TAGS=tag1,tag2,tag3 $java Myapp