Java EE 6 CDI Examples

The first set of samples is based on the samples that ship with Weld, the reference implementation of CDI. This version of the samples include a working Eclipse Java EE IDE project files as well as a simplified maven pom.xml that will work with Maven 2 and Maven 3.

Accessible to all svn, git or just download the zip:

Originally we were hosting the examples on github, but decided that more developers know svn and the developers who know git also know svn so svn was the least common denominator.  Alos we support developers who don’t know svn, by providing a zip file to download. Not everyone uses svn.

Downloading the code with subversion:

To download the code using svn, use this from the command line:

$ svn co http://jee6-cdi.googlecode.com/svn/samples/trunk jee6samples

Downloading the code as a zip file:

To download the code as a zip file click on the following link which will take you to the download page of the project site.

Page that contains downloads for samples

Requirements:

You need a Java 6 EE server. Don’t worry we explain how to download a lightweight one. The samples should work with Resin, Glassfish, JBoss or any other application server that supports the Java EE 6 Web Profile.

You need Java 6 SDK.

If you plan on building with maven, then you need maven (it works with maven 2 and maven 3).

Building with maven:

$ cd jee6samples

$ cd numberguess

$ mvn clean install

At this point you should have a war file that will run in any JEE 6 Web Profile Server container (Resin, JBoss and Glassfish). The war file will be in the ./target director. You can copy this war file to the deployment directory of your server and start it up.

You can build login in a similar manner.

Building with ant:

$ cd jee6samples

$ cd numberguess

$ ant clean package

At this point you should have a war file that will run in any JEE 6 Web Profile Server container (Resin, JBoss and Glassfish). The war file will be in the ./target director. You can copy this war file to the deployment directory of your server and start it up.

You can build login in a similar manner.

Running with Eclipse Java EE IDE:

You can run the examples with Eclipse. We include the jar files you need to build in Eclipse. Then it is just a matter of configuring your server in Eclipse. Don’t worry we will walk you through so you can go from download to up and running in less than 15 minutes.

The first step is to make sure you have a fairly recent version of Eclipse Java EE IDE.

If you don’t have the Java EE IDE from Eclipse, you should download it. As you can see, we used Helios Server Release 2 for this.

If you don’t have Eclipse go to http://www.eclipse.org/downloads/ and download the Java EE IDE.

To import the projects, run the  File->Import menu item.

Then under General select “Existing Projects Into Workspace”.

Next select the root directory of the download.

At this point, the candi-jsf-login and the candi-jsf-numberguess projects should be imported into your Eclipse workspace. All that is left is to install a Server, and run them.

Installing and running a server in Eclipse:

You need a server that is Java EE 6 Web profile compliant. You could use Resin, JBoss or Glassfish or a few others. For this example, we will use Resin because it is lightweight and fast and it just works.

You can download Resin here:

http://www.caucho.com/download/

This example is using the 4.0.X series.

Once you download it, you can configure it in Eclipse, but first you need to install the Resin Plugin for Eclipse WTP. (WTP stands for Web Tools Project and it allows you to use Eclipse with many different sever vendors).

Go to the Help->Install New Software menu item.

Then click o the Add button.

Then enter the Name Caucho (the company that makes Resin) and the Location as http://caucho.com/eclipse as shown below.

Next from the Tree List Select expand the Caucho Resin Node and Select the Resin Eclipse Feature. Then click the Finish button. You have now installed the Resin plugin for Eclipse.

Now to run one of the projects on Resin from Eclipse, you just Right Click the Project and select Run As->Run On Server. The first time you do this you will need to setup a Resin server in Eclipse as follows:

Keep hitting the Next > button until it deploys the application to the server (accept the defaults).

You only have to setup Resin once. Subsequent times, it just works.

At this point you should have a running application.

You can run and debug this application in Eclipse and you can use it as a template to develop your own application to explore CDI and Java EE 6.

Happy coding!

Let us know what you think.

Document History:

Added references. Added additional session on using ant build script.

Additional References:

Author

URL

Date

Description

Site Name

blog

Nicolas Frankel

CDI, an overview – Part 1  

May 22, 2010

 

A Java Geek

 

Nicolas Frankel

CDI, an overview – Part 2 

May 31, 2010

Former Spring Fanboy promotes CDI

A Java Geek

 

Reza Rahman

Dependency Injection in Java EE 6 - Part 1 

November 1, 2009

Introduction to CDI

Caucho and TheServerSide.com

 

Reza Rahman

DI in Java EE 6 - Part 2 

November 1, 2009

Introduction to CDI covers using Instance

Caucho and TheServerSide.com

 

Reza Rahman

DI in Java EE 6 - Part 4 

January 8, 2010

The concept of CDI conversations in detail.

Caucho and TheServerSide.com

 

Reza Rahman

CDI interaction with JSF in detail. 

February 2, 2010

CDI interaction with JSF in detail.

Caucho and TheServerSide.com

 

Reza Rahman

DI in Java EE 6 - Part 3 

February 8, 2010

CDI interceptors allow you to isolate cross-cutting concerns in a very concise, type-safe and intuitive way.

Caucho and TheServerSide.com

 

Adam Bien

What Is The Relation Between JSR-299 and JSR-330 In Java EE 6? Do We Need Two DI APIs?  

January 7, 2010

What Is The Relation Between JSR-299 and JSR-330 In Java EE 6? Do We Need Two DI APIs?  

JavaLobby.com

 

Gavin King et al

Weld - JSR-299 Reference Implementation - Official Documentation 

 

This is the best guide to date on CDI

JBoss