Skip to content
This repository has been archived by the owner on Nov 18, 2020. It is now read-only.
Mahmoud Ben Hassine edited this page Jul 1, 2016 · 11 revisions

What is Easy Properties ?

Easy Properties is a library that allows you to inject configuration properties in your objects in a declarative way using annotations.

Configuration properties can be loaded from a variety of sources:

  • System properties passed to JVM
  • Properties files
  • Resource bundles
  • Database, etc

Usually, we write a lot of boilerplate code to load these properties into objects, convert them to typed values, etc.

The idea behind Easy Properties is to implement the Inversion Of Control principle : Instead of having objects looking actively for configuration properties, these objects simply declare configuration properties they need and these properties will be provided to them by a tool, Easy Properties for instance!

Let's see an example. Suppose you have a Java object of type Bean which should be configured with:

  • An Integer property threshold from a system property passed to the JVM with -Dthreshold=100

  • A String property bean.name from a properties file named myProperties.properties

To load these properties in your Bean object using Easy Properties, you annotate fields to declare needed configuration properties as follows:

public class Bean {

    @Property(source = "myProperties.properties", key = "bean.name")
    private String beanName;

    @SystemProperty(value = "threshold", defaultValue = "50")
    private int threshold;

    //getters and setters omitted

}

and instructs Easy Properties to inject these configuration properties in the annotated fields:

//Instantiate your object
Bean bean = new Bean();

//Create a PropertiesInjector and inject properties in your object
aNewPropertiesInjector().injectProperties(bean);

That's it! Easy Properties will:

  • introspect the Bean instance looking for fields annotated with @Property and @SystemProperty
  • convert each property value to the field's type
  • and inject that value into the annotated field.

Without Easy Properties, you would write something like this:

public class Bean {

    private int threshold;

    private String beanName;

    public Bean() {

        //Load 'threshold' property from system properties
        String thresholdProperty = System.getProperty("threshold");
        if ( thresholdProperty != null ) {
            try {
                threshold = Integer.parseInt(thresholdProperty);
            } catch (NumberFormatException e) {
                // log exception
                threshold = 50; //default threshold value;
            }
        }

        //Load 'bean.name' property from properties file
        Properties properties = new Properties();
        try {
            InputStream inputStream = this.getClass().getClassLoader()
                        .getResourceAsStream("myProperties.properties");
            if (inputStream != null) {
                properties.load(inputStream);
                beanName = properties.getProperty("bean.name");
            }
        } catch (IOException ex) {
            // log exception
            beanName = "FOO"; // default bean name value
        }

    }

    //getters and setters omitted

}

As you can see, a lot of boilerplate code is written to load two properties, convert them to the target field type, etc.

Easy Properties takes care of all this boilerplate for you, which makes your code cleaner, more readable and maintainable.