Injecting configuration values using CDI’s InjectionPoint
Dependency injection is a great technology for the organization of class dependencies. All class instances you need in your current class are provided at runtime from the DI container. But what about your configuration?
Of course, you can create a “Configuration” class and inject this class everywhere you need it and get the necessary value(s) from it. But CDI lets you do this even more fine-grained using the InjectionPoint concept.
If you write a @Produces method you can let your CDI container also inject some information about the current code where the newly created/produced value is inject into. A complete list of the available methods can be found here. The interesting point is, that you can query this class for all the annotations the current injection point has:
Annotated annotated = injectionPoint.getAnnotated(); ConfigurationValue annotation = annotated.getAnnotation(ConfigurationValue.class);
As the example code above shows, we can introduce a simple @Qualifier annotation that marks all the injection points where we need a specific configuration value. In this blog post we just want to use strings as configuration values, but the whole concept can of course be extended to other data types as well. The already mentioned @Qualifier annotation looks like the following one:
@Target({ElementType.FIELD, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Qualifier public @interface ConfigurationValue { @Nonbinding ConfigurationKey key(); } public enum ConfigurationKey { DefaultDirectory, Version, BuildTimestamp, Producer }
The annotation has of course the retention policy RUNTIME because the CDI container has to evaluate it while the application is running. It can be used for fields and methods. Beyond that we also create a key attribute, which is backed by the enum ConfigurationKey. Here we can introduce all configuration values we need. In our example this for example a configuration value for a default directory, for the version of the program and so on. We mark this attribute as @Nonbinding to prevent that the value of this attribute is used by the CDI container to choose the correct producer method. If we would not use @Nonbinding we would have to write a @Produces method for each value of the enum. But here we want to handle all this within one method.
The @Produces method for strings that are annotated with @ConfigurationKey is shown in the following code example:
@Produces @ConfigurationValue(key=ConfigurationKey.Producer) public String produceConfigurationValue(InjectionPoint injectionPoint) { Annotated annotated = injectionPoint.getAnnotated(); ConfigurationValue annotation = annotated.getAnnotation(ConfigurationValue.class); if (annotation != null) { ConfigurationKey key = annotation.key(); if (key != null) { switch (key) { case DefaultDirectory: return System.getProperty("user.dir"); case Version: return JB5n.createInstance(Configuration.class).version(); case BuildTimestamp: return JB5n.createInstance(Configuration.class).timestamp(); } } } throw new IllegalStateException("No key for injection point: " + injectionPoint); }
The @Produces method gets the InjectionPoint injected as a parameter, so that we can inspect its values. As we are interested in the annotations of the injection point, we have a look if the current injection point is annotated with @ConfigurationValue. If this is the case, we have a look at the @ConfigurationValue’s key attribute and decide which value we return. That’s it. In a more complex application we can of course load the configuration from some files or some other kind of data store. But the concept remains the same.
Now we can easily let the CDI container inject the configuration values we need simply with these two lines of code:
@Inject @ConfigurationValue(key = ConfigurationKey.DefaultDirectory) private String defaultDirectory;
Conclusion: Making a set of configuration values accessible throughout the whole application has never been easier.