cdi Getting started with cdi

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


This section provides an overview of what cdi is, and why a developer might want to use it.

It should also mention any large subjects within cdi, and link out to the related topics. Since the Documentation for cdi is new, you may need to create initial versions of those related topics.


CDI is a Java EE specification. It specifies how things should be done, and which features must be provided, but it isn't actually a specific library or set of code. In order to use CDI, you will need to use a CDI implementation.

The reference implementation of the CDI spec is a set of libraries known as Weld. An alternative implementation of the CDI spec exists as Apache OpenWebBeans. Either of these implementations will be able to give you the features of CDI. If you are not using a Java EE application server that ships with one of these implementations, it will be up to you to select and install one of these implementations into your application or runtime.

Installation or Setup

Detailed instructions on getting cdi set up or installed.

Quick setup in a Java SE environment

If you are working with a Java EE 6+ application server, CDI is part of the container and you do not need to do anything to start using it. But CDI is not limited to Java EE application servers. It can be used in Java SE applications or simple servlet containers just as easily. Let's take a look at using CDI in a simple command-line application.

Step 1. Add dependencies to your POM.


Step 2. Add beans.xml

CDI requires an empty beans.xml file so it can scan the JAR for classes. So create


with the following

<beans xmlns=""
        <exclude name="org.jboss.weld.**" />

Step 3. Initialize CDI

In this example, the main(String []) method initializes CDI and then CDI is used to get an instance of the class itself to start running the SE application.

import java.util.Arrays;
import java.util.List;
import javax.enterprise.inject.spi.CDI;
import javax.inject.Inject;

public class Main {
    public static void main(String[] args) {
        CDI<Object> cdi = CDI.getCDIProvider().initialize();
        Main main =;

    protected MyService myService;

    protected void main(List<String> args) {
        System.out.println("Application starting");

        // MyService object injected by CDI

That's it, really simple.

Got any cdi Question?