Quick start guide

First Install Constretto

Constretto is built with maven, so if you are using maven, you can simply add Constretto as dependencies in your pom:

<dependencies>
  ...
  <dependency>
    <groupId>org.constretto</groupId>
    <artifactId>constretto-api</artifactId>
    <version>1.1.3</version>
  </dependency>
  <dependency>
    <groupId>org.constretto</groupId>
    <artifactId>constretto-core</artifactId>
    <version>1.1.3</version>
    <scope>runtime</scope>
  </dependency>
  ...
</dependencies>

If you would like to use the Constretto Junit support with junit 4.4 (spring 2.5.x) add:

<dependency>
  <groupId>org.constretto</groupId>
  <artifactId>constretto-test</artifactId>
  <version>1.1.3</version>
  <scope>test</scope>
</dependency>

If you would like to use the Constretto Junit support with junit 4.5 (spring 3.x) add:

<dependency>
  <groupId>org.constretto</groupId>
  <artifactId>constretto-test-junit-4.5</artifactId>
  <version>1.1.3</version>
  <scope>test</scope>
</dependency>

Repository

To obtain these artifacts - see Maven Repositories

Configure it...

With Spring...

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:constretto="http://constretto.org/schema/constretto"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                          http://constretto.org/schema/constretto
                          http://constretto.org/schema/constretto/constretto-1.2.xsd">

  <constretto:configuration>
    <constretto:stores>
      <constretto:properties-store>
        <constretto:resource location="classpath:properties/test1.properties"/>
      </constretto:properties-store>
    </constretto:stores>
  </constretto:configuration>
</beans>

Or With The Java API

ConstrettoConfiguration config = new ConstrettoBuilder()
  .createPropertiesStore()
    .addResource(new DefaultResourceLoader().getResource("classpath:test1.properties"))
    .done()
  .getConfiguration();

Add Configuration Instructions

public class DataSourceConfiguration {

  private String myUrl;
  private String myPassword;


  @Configure
  public void configure(String url, @Configuration(expression = "password") String secret) {
    this.myUrl = url;
    this.myPassword = secret;
  }

  public String getUrl() {
    return myUrl;
  }

  public String getPassword() {
    return myPassword;
  }
}

Tell Constretto to inject configuration

If you are using spring, this is done automaticly for you, but if you use the Java API, this is done as shown in the example below:

public class MyApp{
  public static void main(String[] args){
    ConstrettoConfiguration configuration = buildConfig();
    MyConfiguredClass myConfiguredClass = new MyConfiguredClass();
    configuration.on(myConfiguredClass);
  }
}

Tag your properties

One of the key features of Constretto is the ability to tag properties in the property files. When no tag is present the value is considered default, but if tagged, it will only be available if your application is started with that tag in the tags list (see next section)

key1 = default value for key1
key2 = default value for key2

@some tag.key1 = value in tag "some tag" for key1
@some tag.key2 = value in tag "some tag" for key2

Tell Constretto What Tags To Use When looking Up Values

java -jar myapp.jar MyMain -DCONSTRETTO_TAGS=development,linux,mock, some tag

or :

export CONSTRETTO_TAGS=development,linux,mock, some tag
java -jar myapp.jar MyMain

That should be it, and your application should start up and be fully configured.