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
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
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="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
</beans>
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>();
@Override
public void registerCourse(String course) {
courses.add(course);
}
@Override
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;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(urlPatterns = {"/courses"})
public class CourseServlet extends HttpServlet {
@Inject
private CourseService courseService;
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
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()){
out.println(course);
}
}
}
Demo
In this example we will deploy our example application on JBoss which will manage the CDI for us.
URL: http://localhost:8080/inject/courses