Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency injection using ".properties" file

I am using Java EE 6 and need to load configuration from a ".properties" file. Is there a recommended way (best practice) to load the values ​​from the configuration file using dependency injection? I found annotations for this in Spring, but I have not found a "standard" annotation for Java EE.

This guy have developed a solution from scratch:

http://weblogs.java.net/blog/jjviana/archive/2010/05/18/applicaction-configuration-java-ee-6-using-cdi-simple-example

"I couldn't find a simple example of how to configure your application with CDI by reading configuration attributes from a file..."

But I wonder if there is a more standard way instead of creating a configuration factory...

like image 289
Paulo N Avatar asked Apr 15 '12 19:04

Paulo N


People also ask

What types does property inject support in Java?

In addition to Strings, Property Inject also supports the injections of all Java primitive types and their wrapper classes, BigInteger, BigDecimal, Date, JsonArray, JsonObject, and java.util.Properties collections themselves. How are you using properties in your CDI-enabled applications today?

What is the difference between dependency property and dependency object?

The name of this type is DependencyProperty. The other important type that defines the WPF property system is DependencyObject, which defines the base class that can register and own a dependency property. Dependency property, which is a property that's backed by a DependencyProperty.

Who can set the value of a dependency property in WPF?

Any of the property-based inputs within the WPF property system can set the value of a dependency property. Dependency property value precedence exists so that the various scenarios for how properties obtain their values interact in a predictable way.

Can an element inherit the value of a dependency property?

An element can inherit the value of a dependency property from its parent in the object tree. Property value inheritance behavior isn't globally enabled for all dependency properties, because the calculation time for inheritance affects performance. Property value inheritance is typically only enabled in scenarios that suggest applicability.


1 Answers

Configuration annotation

package com.ubiteck.cdi;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.enterprise.util.Nonbinding;
import javax.inject.Qualifier;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface InjectedConfiguration {
    /**
     * Bundle key
     * @return a valid bundle key or ""
     */
    @Nonbinding String key() default "";
    /**
     * Is it a mandatory property
     * @return true if mandator
     */
    @Nonbinding boolean mandatory() default false;
    /**
     * Default value if not provided
     * @return default value or ""
     */
    @Nonbinding String defaultValue() default "";
 }

The configuration factory could look like :

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;

public class ConfigurationInjectionManager {
    static final String INVALID_KEY="Invalid key '{0}'";
    static final String MANDATORY_PARAM_MISSING = "No definition found for a mandatory configuration parameter : '{0}'";
    private final String BUNDLE_FILE_NAME = "configuration";
    private final ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_FILE_NAME);

    @Produces
    @InjectedConfiguration
    public String injectConfiguration(InjectionPoint ip) throws IllegalStateException {
        InjectedConfiguration param = ip.getAnnotated().getAnnotation(InjectedConfiguration.class);
        if (param.key() == null || param.key().length() == 0) {
            return param.defaultValue();
        }
        String value;
        try {
            value = bundle.getString(param.key());
            if (value == null || value.trim().length() == 0) {
                if (param.mandatory())
                    throw new IllegalStateException(MessageFormat.format(MANDATORY_PARAM_MISSING, new Object[]{param.key()}));
                else
                    return param.defaultValue();
            }
            return value;            
        } catch (MissingResourceException e) {
            if (param.mandatory()) throw new IllegalStateException(MessageFormat.format(MANDATORY_PARAM_MISSING, new Object[]{param.key()}));
            return MessageFormat.format(INVALID_KEY, new Object[]{param.key()});
        }
    }

Tutorial with explanation and Arquillian test

like image 144
Shrini Gandrange Avatar answered Oct 03 '22 07:10

Shrini Gandrange