Configuring CDI with Tomcat example

23 March 2013
By Gonçalo Marques
In this tutorial you will learn how to configure Contexts and Dependency Injection (CDI) with Tomcat.

Introduction

Tomcat is not a full-blown EE container so we must include some required libraries in order to enable CDI support. In this tutorial we will cover CDI configuration in Tomcat and also provide a working sample.

This tutorial considers the following software and environment:

  1. Ubuntu 12.04
  2. JDK 1.7.0.09
  3. Weld 1.1.10
  4. Tomcat 7.0.35

Configuration

Tomcat is not a full-blown EE container so we must include some required libraries in order to enable CDI support. In this tutorial we will be using Weld - the CDI reference implementation. The required Maven dependencies used in this tutorial are the following:

Required Maven dependencies

<dependencies>
  <dependency>
    <groupId>org.jboss.weld.servlet</groupId>
    <artifactId>weld-servlet</artifactId>
    <version>1.1.10.Final</version>
  </dependency>
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
  </dependency>
</dependencies>


In order to enable CDI we must also place an empty beans.xml file in WEB-INF folder:

/WEB-INF/beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>


The last configuration step is to configure our CDI listener. We do this in web.xml:

/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0">

  <display-name>Tomcat CDI example</display-name>

  <listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
  </listener>

</web-app>

The injected bean

Now let's define the bean that will be injected by CDI in our test servlet. Following is the bean interface:

Service.java

package com.byteslounge.cdi.bean;

public interface Service {

  int doWork(int a, int b);
	
}

Now the bean implementation:

ServiceBean.java

package com.byteslounge.cdi.bean;

public class ServiceBean implements Service {
	
  @Override
  public int doWork(int a, int b) {
    return a + b;
  }

}


Note: We are not defining any specific scope in our bean so this means that the bean will be created with Dependent scope, ie. the bean will have the same scope as its target (or in other words the same scope of the component where it will be injected).

A simple test servlet

Now let's define a simple servlet where we will inject our bean:

TestServlet.java
package com.byteslounge.cdi.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.byteslounge.cdi.bean.Service;

@WebServlet(name = "testServlet", urlPatterns = {"/testcdi"})
public class TestServlet extends HttpServlet {

  private static final long serialVersionUID = 2638127270022516617L;
  
  @Inject
  private Service service;
  
  protected void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    
    int a = 2;
    int b = 3;
    
    PrintWriter out = response.getWriter();
    out.println("Hello World: " + service.doWork(a, b));
    out.close();
  }

}

Note that the private property service is annotated with @Inject. This means that when the container is initializing the servlet it will look for a Service implementation. It will find our implementation - ServiceBean - and inject it into the servlet.

When we deploy the application in Tomcat and access the servlet mapped URL we will get the following output:

CDI example application output

The tutorial source code is available for download at the end of this page.

Download source code from this article

Related Articles

Comments

About the author
Gonçalo Marques is a Software Engineer with several years of experience in software development and architecture definition. During this period his main focus was delivering software solutions in banking, telecommunications and governmental areas. He created the Bytes Lounge website with one ultimate goal: share his knowledge with the software development community. His main area of expertise is Java and open source.

GitHub profile: https://github.com/gonmarques

He is also the author of the WiFi File Browser Android application: