Skip to content
This repository has been archived by the owner on Nov 18, 2020. It is now read-only.
benas edited this page Nov 13, 2014 · 11 revisions

What is @dp4j?

Annotation Driven Properties For Java 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 ADP4J 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, ADP4J 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 ADP4J, 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 ADP4J to inject these configuration properties in the annotated fields :

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

//Instantiate ADP4J properties injector
PropertiesInjector propertiesInjector = new PropertiesInjectorBuilder().build();

//Inject properties in your object
propertiesInjector.injectProperties(bean);

That it! ADP4J will introspect the Bean type instance looking for fields annotated with @Property and @SystemProperty, convert each property value to the field type and inject that value into the annotated field.

Without ADP4J, 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 plumbing code is written to load two properties, convert them to the right type, etc.

ADP4J handles all this plumbing for you, which makes your code cleaner, more readable and maintainable.