This post demonstrates how Dependency Injection
works with JavaEE
CDI
and Tomcat 8
. We’ll display some text from a bean injected into our Servlet
via CDI
.
As we know, Tomcat
is just a Servlet Container
and does not have other JavaEE
components available out-of-the-box. Tomee
may be a better alternative but should we go all-out “Enterprise”, choose a JavaEE
-compliant Application Server like JBoss WildFly
or IBM Websphere
.
Requirements
Stuff used in this post.
- Eclipse Mars.2 Release (4.5.2)
- Java 8
- Windows 10 64bit
- Tomcat 8.0.39
- Maven 3.3.3 (Embedded in Eclipse)
Key Maven Dependencies
These are the key dependencies.
javaee-web-api
[wp_ad_camp_4]
This contains most API
related to Web Applications.
1 2 3 4 5 6 | <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> |
cdi-api
This is the CDI API.
1 2 3 4 5 6 | <dependency> <groupId>javax.enterprise</groupId> <artifactId>cdi-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> |
weld-servlet
A reference implementation of CDI
on a Servlet Container
.
[wp_ad_camp_3]
1 2 3 4 5 | <dependency> <groupId>org.jboss.weld.servlet</groupId> <artifactId>weld-servlet</artifactId> <version>2.4.4.Final</version> </dependency> |
CDI Bean
This bean is created in the CDI
“container” and injected into our Servlet
. We used @ApplicationScoped
and the bean is available until Tomcat/Web Application
is shutdown.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.turreta.cdi.servlet.beans; ... @Named @ApplicationScoped public class AppDetailBean implements Serializable { private String appName; private String version; @PostConstruct public void init() { appName = "Turreta.com Tomcat/CDI Demo"; version = "1.0"; } public String getAppName() { return appName; } public String getVersion() { return version; } } |
Our Servlet
[wp_ad_camp_2]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package com.turreta.cdi.servlet.demo; ... import com.turreta.cdi.servlet.beans.AppDetailBean; @WebServlet(urlPatterns = "/miservlet") public class MyServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Inject private AppDetailBean appDetailBean; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* I know it's old-school! Apologies :) */ response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>App Detail</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>App Name:" + appDetailBean.getAppName() + "</h1>"); out.println("<h2>Version:" + appDetailBean.getVersion() + "</h2>"); out.println("</body>"); out.println("</html>"); } } |
Configuration and other XML files
There are other configurations that are needed before we could run and test our simple web application.
Define beans.xml
This file contains the following and should be placed in "Deployed Resources"/webapp/WEB-INF
.
1 2 3 4 5 6 7 8 | <?xml version="1.0" encoding="UTF-8"?> <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" bean-discovery-mode="annotated" > </beans> |
Define web.xml
[wp_ad_camp_1]
Same with beans.xml
, this files should be placed "Deployed Resources"/webapp/WEB-INF
.
1 2 3 4 5 6 7 8 9 10 11 12 | <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <resource-env-ref> <!-- Enable Weld CDI, also needs META-INF/context.xml entry --> <resource-env-ref-name>BeanManager</resource-env-ref-name> <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type> </resource-env-ref> </web-app> |
Define context.xml
This file should be placed under "Deployed Resources"/web-resources/META-INF
.
1 2 3 4 5 6 7 8 9 | <?xml version="1.0" encoding="UTF-8"?> <Context antiJARLocking="true" path="/com-turreta-cdi-servlet-demo"> <Resource name="BeanManager" auth="Container" type="javax.enterprise.inject.spi.BeanManager" factory="org.jboss.weld.resources.ManagerObjectFactory" /> </Context> |
Testing
The web application is launched from Eclipse
.
Our servlet
outputs this page.
[wp_ad_camp_5]
Download the codes
https://github.com/Turreta/Tomcat-8-and-Java-EE-CDI