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.


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
  3. Weld 1.1.10
  4. Tomcat 7.0.35


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


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


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" 

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


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
  id="WebApp_ID" version="3.0">

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



The injected bean

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

package com.byteslounge.cdi.bean;

public interface Service {

  int doWork(int a, int b);

Now the bean implementation:

package com.byteslounge.cdi.bean;

public class ServiceBean implements Service {
  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:
package com.byteslounge.cdi.servlet;


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;
  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));


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


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:

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