Injection using a .properties file.

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

This guy developed the solution from scratch:

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

"I could not find a simple example of setting up an application with CDI by reading configuration attributes from a file ..."

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

+6
source share
4 answers

Configuration Annotations

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 factory configuration might look like this:

 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()}); } } 

Explanation Tutorial and Arquillian Test

+3
source

Although this does not fit your question, this part of the Weld documentation may be of interest to you.

Mentioning this - no, there is no standard way to inject arbitrary resources / resource files. I assume that this simply goes beyond the specification to standardize such a requirement, depending on individual requirements (Spring is not a specification, they can simply implement whatever they like). However, what CDI provides is aka typesafe mechanism for setting beans configuration holding on the one hand, and a flexible manufacturer mechanism for reading and creating such beans on the other. This is definitely the recommended method you requested.

The approach to which you are attached is certainly very good, even if it may be too much for your needs, depending on the type of properties that you plan to introduce.

A very CDI way to continue is to develop a CDI extension (which will encapsulate all the necessary classes nicely) and deploy it regardless of your projects. Of course, you can also contribute to the CDI extension directory or even Apache Deltaspike .

+1
source

The only “standard” way to do this is to use a qualifier with a non-binding annotation element and make sure that all of your injections depend on the scope. Then from your producer, you can take possession of InjectionPoint and get the key to the qualifier at the injection point. You need something like this:

 @Qualifier public @interface Property { @Nonbinding String value default ""; } ... @Inject @Property("myKey") String myKey; ... @Produces @Property public String getPropertyByKey(InjectionPoint ip) { Set<Annotation> qualifiers = ip.getQualifiers // Loop through qualifers looking for Property.class save that off return ResourceBundle.getBundle(...).getString(property.key); } 

Obviously, there are some improvements you can make for this code, but that should be enough to get you started on the right track.

+1
source

See @ConfigProperty Apache DeltaSpike

+1
source

Source: https://habr.com/ru/post/913263/


All Articles