Property file configuration using Injection (CDI)
|
|
|
Inject configuration using CDI
One of my favorite games in recent times is to minimize the number of libraries as much as possible by using the features provided in the JDK.
In this context, Spring is an excellent candidate. In this article, I will illustrate how to inject the configuration parameters using CDI instead of Spring IOC.
Our requirements are :
Injecting configuration using a configuration file using the parameter key. We want to define a default value if the param is not suported by the configuration file and a mandatory parameter to throw an exception if the parameter is not provided.
Let's define the annotation we will use
Injected configuration annotation
So we are defining the three attributes defined in our requirements and we use the RUNTIME retention policy to get access to it at runtime.
We can bind our configuration using the following snippet :
In this snippet we are injecting the host name using the key 'host.name'. If the parameter is not defined we will use 'localhost' instead.
Configuration Injection Manager
Now that we have our annotation we must process thos annotations to inject the corresponding configuration. To do this, we will develop a "Producer" which we call "ConfigurationInjectionManager".
In this so called "producer" we have a method responding to the @InjectionConfiguration annotation by injecting the corresponding configuration. The method should be marked with the @Produces annotation to be considered as a provider for Injected Configuration. The implementation define the logic and retrieve the configuration using the bundle name defined by the variable BUNDLE_FILE_NAME.
Our implementation is completed but does'it work ? To make sure we have a working implementation we will use the excellllllllent Arquillian framework
Testing injection using Arquillian
With the following snippet we will bundle our solution in an archive and test it using Arquillian.
In createTestArchive method we are creating the archive to deploy. We are adding the annotation class InjectedConfiguration, the configuration manager named "ConfigurationInjectionManager"� and the configuration file called "configuration.properties". To activate CDI we added a blank beans.xml file as a manifest resource.
In line 26 to 32 we are injecting the params to be tested
test_property_injection method is testing a simple workinf case. The test_invalid_key method make sure your get an invalid message in the variable if the configuration parameter is invalid.
Conclusion
CDI is a very elegant way to inject the configuration but it's as wel a very effective way to replace your configuration for testing purpose or refactoring your configuration using an annotation processor.
If you have any remark or questions feel free to put a comment.If you enjoyed this tutorial and want to promote it don't hesitate to click on
Troubleshooting
If your container is using Weld you can get a WELD-001408 Unsatisfied dependencies for type... error .... It means simply that no producer has been found in the classloader of your container to be inject in the injection point defined in the error. Make sure your producer can be found by the classloader and this error should be fixed.
Tags: spring , configuration , parameters , context , injection , excellent , inject