Tutorial: Writing your own CDI extension

Today I will show you how to write a CDI extension.

CDI provides a easy way for extending the functionality, like

  • adding own scopes,
  • enabling java core classes for extension,
  • using the annotation meta data for augmentation or modification,
  • and much more.

In this tutorial we will implement an extension that will inject properties from a property file, as usual I will provide the sources at github [Update: sources @ github].

The goal

Providing an extension that allows us to do the following

where version and appname are defined in the file myProps.txt.


At first we need the dependency of the CDI api

Now we can start. So let’s

Getting wet

The basics

The entry point for every CDI extension is a class that implements  javax.enterprise.inject.spi.Extension

Additionally we must add the full qualified name of this class to a file named  javax.enterprise.inject.spi.Extension in META-INF/services directory.

These are the basic steps for writing a CDI extension.

Background information
CDI uses Java SE’s service provider architecture, that’s why we need to implement the marker interface and adding the file with the FQN of the implementing class.

Diving deeper

Now we must choose the right event to listen for.

Background information
The CDI specs defines several events which are fired by the container during the initialization of the application.
For example the BeforeBeanDiscovery is fired before the container starts with the bean discovery.

For this tutorial we need to listen for the ProcessInjectionTarget event. This event is fired for every single java class, interface or enum that is discovered and that is possibly instantiated by the container during runtime.
So let’s add the observer for this event:

The ProcessInjectionTarget grants access to the underlying class via the method getAnnotatedType and the instance in creation via getInjectionTarget. We use the annotatedType for getting the annotations on the class to check if @PropertyFile is available. If not, we will return directly as a short circuit.
The InjectionTarget is later used for overwriting the current behavior and setting the values from the properties file.

For the sake of this tutorial we assume that the properties file is located directly in the root of the classpath. With this assumption we can add the following code to load the properties from the file

Now we can assign the property values to the fields. But for CDI we have to do this in a slightly different way. We should use the InjectionTarget and override the current AnnotatedType. This allows CDI to ensure that all things could happen in a proper order.

For achieving this we use a final Map<Field, Object> where we can store the current assignments for later usage in the InjectionTarget. The mapping is done in the method assignPropertiesToFields.

As a last step we will now create a new InjectionTarget to assign the field values to all newly created instance of the underlying class.

That’s all for doing the magic. Finally here is the complete code of the ProperyLoaderExtension.


The complete source code will be available on git hub until Monday evening. The jar archive is available here PropertyLoaderExtension.

Final notes

This tutorial has shown how simply you can add a new features to the CDI framework.
Within a few lines of code a working property loading and injection mechanism was added. The events that are fired during the lifecycles of an application provides a loose coupled and powerful approach to add new features, intercept bean creation or changing the behavior.

The property injection could also be achieved by introducing a set of producer methods, but this approach requires one producer method per field type. With this general approach you just have to add new converters for enabling injection of other types of values.

4 Kudos

One thought on “Tutorial: Writing your own CDI extension

  1. Pingback: Released : CDIProperties – v.1.0.0 | Coders Kitchen

Leave a Reply

Your email address will not be published. Required fields are marked *