adam bien's blog

Injecting Properties Into Java EE Applications 📎

To make basic datatypes injectable into POJOs, like:


    @Inject
    private String greeting;

    @Inject
    private int intValue;


...you will have to expose them first. The name of field may act as the lookup key:


//...

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Inject;

public class Configurator {

	//...

    @Inject
    Instance<Map<String, String>> initialValues;

    public void init() {
        this.store = //...
        for (Map<String, String> initial : initialValues) {
            this.store.putAll(initial);
        }
    }


    @Produces
    public String getString(InjectionPoint ip) {
        String className = ip.getMember().getDeclaringClass().getName();
        String key = className + "." + ip.getMember().getName();
        String fieldName = computeKeyName(ip.getAnnotated(), key);
        return this.store.get(fieldName);
    }

    String computeKeyName(Annotated annotated, String key) {
        Configurable annotation = annotated.getAnnotation(Configurable.class);
        return annotation == null ? key : annotation.value();

    }

    @Produces
    public long getLong(InjectionPoint ip) {
        String stringValue = getString(ip);
        if (stringValue == null) {
            return 0;
        }
        return Long.parseLong(stringValue);
    }
}


The conventional field name as lookup key can be overridden with an annotation:

    @Inject
    @Configurable("msg")
	String message;


The annotation is expects a single string which is going to be used as a key:


@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Configurable {
    String value();
}

Now you only need to expose a datasource of your choice as Map<String, String>, like e.g. environment variables or System-properties to make them injectable:


import javax.enterprise.inject.Produces;

public class Initializer {

    @Produces
    public Map<String, String> getInitialConfiguration() {
    	//...fetch properties from wherever you like
    }
}

"How to inject properties into Java EE apps" was one of the questions in the recent airhacks.tv.

The code above was taken from JCache Configurator for Java EE -- a one-class Java EE framework. See also the Java Magazine article Convention Over Configuration in Java EE 6.

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.