You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2012/01/27 08:13:17 UTC
svn commit: r1236551 - /openejb/site/trunk/content/tomcat-cdi.mdtext
Author: dblevins
Date: Fri Jan 27 07:13:16 2012
New Revision: 1236551
URL: http://svn.apache.org/viewvc?rev=1236551&view=rev
Log:
More details on CDI
Modified:
openejb/site/trunk/content/tomcat-cdi.mdtext
Modified: openejb/site/trunk/content/tomcat-cdi.mdtext
URL: http://svn.apache.org/viewvc/openejb/site/trunk/content/tomcat-cdi.mdtext?rev=1236551&r1=1236550&r2=1236551&view=diff
==============================================================================
--- openejb/site/trunk/content/tomcat-cdi.mdtext (original)
+++ openejb/site/trunk/content/tomcat-cdi.mdtext Fri Jan 27 07:13:16 2012
@@ -2,4 +2,53 @@ Title: Tomcat CDI
Tomcat + Java EE = TomEE, the Java Enterprise Edition of Tomcat. With TomEE you get Tomcat with CDI added and integrated and ready to go!
+ import javax.annotation.Resource;
+ import javax.enterprise.inject.spi.BeanManager;
+ import javax.inject.Inject;
+ import javax.servlet.ServletException;
+ import javax.servlet.http.HttpServlet;
+ import javax.servlet.http.HttpServletRequest;
+ import javax.servlet.http.HttpServletResponse;
+ import java.io.IOException;
+
+ public class MyServlet extends HttpServlet {
+
+ @Inject
+ private Car car;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+
+ car.drive();
+
+ }
+ }
+
+Where `Car` could be any POJO with a no-arg constructor. Additionally it's quite easy to create a factory to create the car you want injected. Simply do as follows:
+
+ import javax.enterprise.inject.Produces;
+
+ public class AssemblyLine {
+
+ @Produces
+ public Car createFancyCar() {
+ return new Car("Ferrari", "458 Italia", 2012);
+ }
+ }
+
+So when will the `Car` be created? In the above it will be created when `MyServlet` is created. If you annotate `createFancyCar` with `@RequestScoped` it will be created once
+per http request and shared by everyone in the request. If you annotate `createFancyCar` with `@SessionScoped` it will be created once
+per `HttpSession` and shared by everyone in the same session. So no more repeatedly putting and getting into the `HttpSession` by hand, just let the container do it!
+
+No need to add even a single library! To make the above work all you need is a `META-INF/beans.xml` file in your webapp like the following:
+
+ <beans/>
+
+Any jar in your webapp with the above will automatically be CDI enabled and all those beans can be injected with `@Inject` with no need to make them EJBs or third party frameworks.
+
+All this is setup and ready to go! Spend your time learning and having fun and writing your app, don't
+spend it chasing down libraries and integrating things the hard way.
+
+[Download](downloads.html) TomEE and you're minutes away from having fun with CDI on Tomcat.
+
{include:apache-tomee.mdtext}
\ No newline at end of file