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 Jun 8, 2017 · 11 revisions

What is Easy Props?

Easy Props is a library to inject configuration properties in Java objects declaratively using annotations. Let's see a quick example. Suppose you have an 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 the Bean object using Easy Props, you annotate fields 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 instruct the library to inject these 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 Props will:

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

You can also configure your object at construction time:

public class Bean {

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

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

    public Bean () {
        aNewPropertiesInjector().injectProperties(this);
    }

    //getters and setters omitted

}

Now just create your object and it will be configured and ready to use.

Without Easy Props, 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 just two properties, convert them to the target field type, etc. Easy Props takes care of all this boilerplate with a couple of intuitive annotations, which makes your code cleaner, more readable and maintainable.