Building a CDI 2 standalone Java application

In the second article about CDI 2 we will learn how to create a standalone J2SE application featuring CDI2. Let’s get started!

Using Context Dependency Injection for J2SE application is something not totally new to developers. For example,before CDI 2 you could start-up the Weld CDI container using the specific classes provided by it. This needed to include at first the correct dependencies:


Then you could startup the container with like this:

public static void main(String[] args) throws IOException {
    Weld weld = new Weld();
    WeldContainer container = weld.initialize();
    Application application = container.instance().select(Application.class).get();;

Although that does work, it was needed to have a standard way to boot your CDI containers, once that you have available in your classpath the right dependencies. As CDI 2 is progressing quickly ( to the version 2.0 let’s see how you can bootstrap the Weld container with a J2SE project.

The class we will use for this purpose is which is a CDI container initializer for Java SE. By using it, we will be able to create an instance of a which is your gate for accessing the container in Java SE.

Let’s see an HelloWorld CDI 2 example:

package com.mastertheboss;

import javax.enterprise.inject.spi.*;

public class CDI2Demo {
	public static void main(String... args) {
		SeContainerInitializer containerInit = SeContainerInitializer.newInstance();
		SeContainer container = containerInit.initialize();

		// Fire synchronous event that triggers the code in App class.
		container.getBeanManager().fireEvent(new SimpleEvent());


So, once we have inited the SeContainer we fire a synchronous event that triggers the code in App class:

package com.mastertheboss;

import javax.enterprise.event.*;

public class App {
    public void onEvent(@Observes SimpleEvent ignored, SimpleService service) {

SimpleEvent is just a no-implementation Event:

package com.mastertheboss;

public class SimpleEvent {


The observed event will execute the greet() method in SimpleService:

package com.mastertheboss;

import javax.inject.*;

public class SimpleService {
    private Hello greeter;

    public void greet()

Just to add a taste of CDI, we’ve injected the Hello Bean in it which will print the final “Hello World” message:

package com.mastertheboss;

public class Hello {
	    public void greet() {
	        System.out.println("Hello World!");

In order to compile and run the HelloWorld project you need mainly two dependencies in your pom.xml:

  • cdi-api 2.0
  • a CDI container implementation such as weld-se-core
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">

Build and run the HelloWorld project with:

$ mvn clean install

And here’s your first glorious CDI 2 standalone application:

CDI 2 standalone J2SE tutorial

Once more thing you could try is building an all-in-one archive with includes your application class and CDI implementations. This can be done by Eclipse by exporting your project as a fat JAR which includes all the required dependencies. The only workaround you have to include is excluding Weld’s classes from being scanned otherwise you will get validation errors. You can do that by including in your beans.xml the following scan rule:

<beans xmlns=""
       version="1.1" bean-discovery-mode="all">
        <exclude name="org.jboss.weld.**" />

Here’s the full project ready for download: