How to @Inject Property File Properties With CDI

DZone 's Guide to

How to @Inject Property File Properties With CDI

Learn how to @inject Property files for your Java app with CDI in this neat tutorial, with annotations, producers, and more!

· Java Zone ·
Free Resource


You use CDI for your Java application and want to use a Property file for some needed configuration, but don't know how to do this...


Create @Producer(s) and an annotation to direct it.


package nl.ivonet.config;

import javax.enterprise.util.Nonbinding;
import javax.inject.Qualifier;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

 * Represents an property key to be injected
public @interface Property {
    @Nonbinding String value() default "";
    @Nonbinding boolean required() default true;


The examples provided here enable parsing of Strings, Booleans and Integers but you can, of course, add producers as needed. Note that the getKey method first looks if the @Property annotation has a value and if so it will take that as the key for the property file, but if you don't provide a value it will take the annotated field name as the key.

package nl.ivonet.config;

import javax.annotation.PostConstruct;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertyProducer {
    private Properties properties;

    public String produceString(final InjectionPoint ip) {
        return this.properties.getProperty(getKey(ip));

    public int produceInt(final InjectionPoint ip) {
        return Integer.valueOf(this.properties.getProperty(getKey(ip)));

    public boolean produceBoolean(final InjectionPoint ip) {
        return Boolean.valueOf(this.properties.getProperty(getKey(ip)));

    private String getKey(final InjectionPoint ip) {
        return (ip.getAnnotated()
                  .isAnnotationPresent(Property.class) && 
                    value().isEmpty()) ? ip.getAnnotated()
                                       : ip.getMember()

    public void init() {
        this.properties = new Properties();
        final InputStream stream = PropertyProducer.class
        if (stream == null) {
            throw new RuntimeException("No properties!!!");
        try {
        } catch (final IOException e) {
            throw new RuntimeException("Configuration could not be loaded!");


  private String rootFolder;


  private String rootFolder;



Extra Consideration

Note that when using this method for injecting Strings and stuff it might get difficult to write Unit tests as Mocking frameworks cannot Mock final classes (e.g. String). If you get to this problem you might want to look at this article.

Have fun,


cdi ,dependecy injection ,java ,property

Published at DZone with permission of Ivo Woltring . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}