Skip to content
Michael Edgar edited this page Jun 6, 2018 · 5 revisions

Welcome to the property-inject wiki!

Property Inject is simple CDI extension to support injection of java.util.Properties values. See Usage for examples.

Before & After

Before - Simplify code like this to retrieve a value from a configuration file:

package com.example;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class SampleBean {
    private String configValue;

    public SampleBean() {
        Properties beanConfig = new Properties();
        ClassLoader loader = getClass().getClassLoader();
        String resourceName = "com/example/SampleBean.properties";

        try (InputStream in = loader.getResourceAsStream(resourceName)) {
            beanConfig.load(in);
        } catch (IOException e) {
            throw new RuntimeException("Fatal error");
        }

        configValue = beanConfig.getProperty("configValue");
    }

    public String getConfigValue() {
        return configValue;
    }
}

After - Values are injected directly into fields and parameters:

package com.example;

import javax.inject.Inject;

import io.xlate.inject.Property;

public class SampleInjectBean {
    @Inject
    @Property
    private String configValue;

    public String getConfigValue() {
        return configValue;
    }
}
Clone this wiki locally