Use CDI and JPA to develop servlet applications

Source: Internet
Author: User
Tags final java web jboss

Before you start

About CDI

The JSR-299 specification, formerly called Web Beans, is designed to consolidate JSF and EJB. By the time the specification was formally released in 2009, JSR-299 had been renamed "contexts and Dependency injection for the Java EE platform" (CDI), and the integration of JSF and EJB was still the goal, but it The only feature that is not CDI.

Read through the text of the JSR-299 specification, most described as a dependency injection (Dependency injection, DI) container. CDI's DI has a different place, its first letter C, representing contexts, is the context and scope. The object that CDI injects must be associated with a context whose lifecycle is closely related to the scope of the WEB application.

This tutorial is suitable for the reader object

Readers of this tutorial need to have the following basics:

familiar with Servlet development. The application logic of the example is relatively simple and can be understood with the Servlet programming basis.

familiar with JPA development. You need to have some understanding of the persistence context and Entity Manager.

Familiar with Maven tools. The sample application in this tutorial configures Pom.xml, and for first contact with MAVEN readers, there may be more problems when updating a dependent library for the initial time, and you need to understand the basics of Maven to resolve it.

Learn about Spring IoC. The CDI and spring IOC are often compared in tutorials, and readers who are unfamiliar with spring IOC can skip these things.

Content Selection for this tutorial

This tutorial explores the use of CDI in a Servlet environment and does not use JSF and EJB. This is mainly due to the following considerations:

JSF is encapsulated deep in the servlet and is not as intuitive as a servlet when dealing with various scopes.

The Servlet is an essential knowledge skill for every Java Web developer, and relatively few developers are familiar with JSF.

The use of CDI in a Servlet environment is relatively low, combining JPA to achieve many of the key features of the JSF + EJB portfolio.

The JSR-346, the CDI 1.1 specification, has been drafted to enhance support for the Servlet.

The content of this tutorial is organized

This tutorial basically organizes knowledge points in a project-driven manner, using the example application that enables the viewing and modification of employee information. The CDI features involved include:

CDI as a dependency injection container for the characteristics and usage.

Implementation of declarative transaction management with CDI.

With CDI implementation in the session scope to share persistence context.

An event that listens on the CDI container.

Development environment Configuration

The following shows the version of the development environment and tool software used for example applications:

Tomcat, Version 7.0.28.

Eclipse Java EE IDE for WEB developers, version Indigo SR2.

Maven, Version 3.0.4.

Maven Configuration

The example application uses Weld and Hibernate, using Maven to manage the project, adding the JBoss public warehouse and related dependencies, and the related configuration code in Pom.xml is shown in Listing 1.

Listing 1. Pom.xml Configuration Code Fragment

<repositories> <repository> <id>jboss-public-repository-group</id> <nam E>jboss Public Repository group</name> <url>http://repository.jboss.org/nexus/content/groups/publi C/</url> <layout>default</layout> ... </repository> </repositories> ;d ependencies> <dependency> <groupId>org.jboss.weld.servlet</groupId> <arti Factid>weld-servlet-core</artifactid> <version>1.1.8.Final</version> </dependency&gt 
    ; <dependency> <groupId>org.jboss.weld</groupId> &LT;ARTIFACTID&GT;WELD-CORE&LT;/ARTIFAC tid> <version>1.1.8.Final</version> </dependency> <dependency> < Groupid>org.hibernate</groupid> <artifactId>hibernate-entitymanager</artifactId> &L t;version>4.1.4.final</version> </dependency> ... </dependencies> 

After the initial download of the project, can be carried out to the Engineering directory: MVN dependency:resolve, let maven download the dependent package.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.