Java EE Context and Dependency Injection @Inject Example

This tutorial we will show how to use the (CDI) Context and Dependency Injection @Inject annotation. CDI dependency injection is the ability to inject beans into others in a typesafe way, which means no XML or string classes but annotations. In a Java EE environment which is an environment managed by the container you don’t need to construct dependencies, just let the container inject a reference for you.

Maven dependencies


Add beans.xml to WEB-INF or META-INF folder

If using CDI (Context and Dependency Injection) the deployment descriptor is called beans.xml and is mandatory. It is essential to enable CDI. During the bean discovery phase, CDI will turn your POJOs into CDI Beans. At Deployment time CDI checks all of your application’s jar and war files and when it finds a beans.xml deployment descriptor, even if it is completely empty (0 bytes) it manages all the POJOs. When you have multiple jar or war files on your classpath and you want to enable CDI management, then all of them must include a beans.xml file either in the WEB-INF or the META-INF folder.

<beans xmlns=""
       version="1.1" bean-discovery-mode="all">

Defining our interface

When CDI is enabled everything becomes container managed as long as you don’t use the new keyword. Instead let the container inject a reference for you.

package com.memorynotfound.cdi;

import java.util.List;

public interface CourseService {

    void registerCourse(String course);

    List<String> fetchAllCourses();


Add the implementation

package com.memorynotfound.cdi;

import java.util.ArrayList;
import java.util.List;

public class CourseServiceImpl implements CourseService {

    private static List<String> courses = new ArrayList<String>();

    public void registerCourse(String course) {

    public List<String> fetchAllCourses() {
        return courses;

Context and Dependency injection @Inject annotation

Now we are ready to inject our CourseService into a bean. This can be achieved by simply using the @Inject annotation on the property, then the container can inject a reference of the CourseService implementation into the courseService property which is called the injection point.

package com.memorynotfound.cdi;

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;

@WebServlet(urlPatterns = {"/courses"})
public class CourseServlet extends HttpServlet {

    private CourseService courseService;

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();

        courseService.registerCourse("Learning CDI @Inject");

        out.println("<h1>Java EE: Context Dependency Injection @Inject annotation</h1>");
        for (String course : courseService.fetchAllCourses()){



In this example we will deploy our example application on JBoss which will manage the CDI for us.

URL: http://localhost:8080/inject/courses context and dependency injection @inject example



You may also like...