Java EE 6 CDI example application

In this tutorial we will show how to upgrade our Java EE 6 EJB based application into a CDI based application using Eclipse and Eclipse Maven plugin.

Contexts and Dependency Injection(CDI) is a new addition to the Java EE specification as of Java EE 6. It provides several benefits that were missing to Java EE developers, such as allowing any JavaBean to be used as a JSF managed bean, including stateless and stateful session beans.

CDI elementary unit is still the Bean. Compared with EJBs, CDI features a different, more flexible kind of Bean: one of the most important differences between the two approaches is that CDI Beans are contextual that s they live in a well defined scope.

In this tutorial we will create a CDI based application using Eclipse and Maven Eclipse plugin.

Start by creating a new Maven Project from the File Menu:

java ee 6 tutorial CDI maven eclipse plugin

Now move to the Archetype selection screen and enter the maven-archetype-webapp which will produce a standard Java EE Web application project which is suited as well for applications containing EJBs and CDI beans:

java ee 6 tutorial CDI maven eclipse plugin

Next choose as Artifact Id "javaee6example" which will be our Project name and enter a Package & Group Id for your project:

newproject3

Ok. Now you should have in your project explorer a basic skeleton of Java EE project:

javaee6example
¦   pom.xml
¦
+---src
¦   +---main
¦       +---java
¦       ¦
¦       +---resources
¦       ¦
¦       +---webapp
¦           ¦
¦           +---WEB-INF
¦                   faces-config.xml
¦                   web.xml
¦
+---target

The first thing to settle is Maven pom.xml which will contain the required dependencies and the plugins which are necessary to deploy the application on JBoss AS 7.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mastertheboss</groupId>
    <artifactId>javaee6example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>Java EE 6 webapp project</name>
    <description>A starter Java EE 6 webapp project for use on JBoss AS 7, generated from the jboss-javaee6-webapp archetype</description>

    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jboss.home>C:\jboss-as-7.1.1.Final</jboss.home>
        <maven.build.timestamp.format>yyyyMMdd'T'HHmmss</maven.build.timestamp.format>

    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jboss.spec</groupId>
                <artifactId>jboss-javaee-web-6.0</artifactId>
                <version>2.0.0.Final</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.spec.javax.annotation</groupId>
            <artifactId>jboss-annotations-api_1.1_spec</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.spec.javax.ws.rs</groupId>
            <artifactId>jboss-jaxrs-api_1.1_spec</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.spec.javax.ejb</groupId>
            <artifactId>jboss-ejb-api_3.1_spec</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>

        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- Compiler plugin enforces Java 1.6 compatibility and activates annotation 
                processors -->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <!-- Java EE 6 doesn't require web.xml, Maven needs to catch up! -->
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.jboss.as.plugins</groupId>
                <artifactId>jboss-as-maven-plugin</artifactId>
                <version>7.1.1.Final</version>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <!-- The default profile skips all tests, though you can tune it to run 
                just unit tests based on a custom pattern -->
            <!-- Seperate profiles are provided for running all tests, including Arquillian 
                tests that execute in the specified container -->
            <id>default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.4.3</version>
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

Please notice that this pom.xml does not contain dependency versions for the single libraries. The dependency version is resolved using a BOM (Bill of materials) which specifies the exact version for the Java EE stack. See this tutorial for more info about it.

Follow us on Twitter