December 20, 2014
Hot Topics:

CDI and EJB 3.1: Complementary Technologies in the Java EE 6 Platform

  • April 25, 2011
  • By Sangeetha S
  • Send Email »
  • More Articles »

Contexts and Dependency Injection (referred to as CDI) is a new specification feature introduced in Java EE 6 Platform. This was earlier referred to as Web Beans and in due course, the name has been changed to Contexts and Dependency Injection. The primary objective of CDI specification is to bring together the Web tier and the transactional services of the Java EE platform (i.e., the idea are to bring the entire transactional services to the Web tier). CDI services facilitate the usage of Enterprise Beans in the JavaServer Faces technology. CDI provides the power of dependency injection and flexibility.

Enterprise JavaBeans 3.1 (EJB 3.1), the latest release as part of Java EE 6 Platform specification, makes the development much simpler and easier. EJB 3.1 specification has simplified a number of features and provides a good declarative support for transactions and security.

CDI and EJB 3.1 both are part of the Java EE 6 platform, act as complementary technologies. In this article, let's try to understand the synergy between both the technologies by starting with an introduction to CDI and EJB 3.1 and let's see how they complement each other in building powerful Web applications.

Contexts and Dependency Injection (CDI): An Introduction

CDI (JSR 299) is a specification that defines a powerful set of services for the Java EE environment that helps in developing Web applications easily. CDI helps to develop Java EE components that exist within the life cycle of an application with well defined scopes. In the Java EE Platform, there is a strong support for transactions in the business tier and persistence tier through the technologies like Enterprise JavaBeans and Java Persistence API. However, there is less/no support for transactions from the Web tier. They are more focused on displaying the presentation content and have limited access to transactional resources. CDI services help in unifying Enterprise JavaBeans (EJB) and JavaServer Faces (JSF) programming models. CDI services allow Enterprise JavaBeans to be used as the Managed Beans in JavaServer Faces framework. CDI also provides a good support for accessing transactional resources which facilitates in easy creation of Web applications using Java Persistence API.

The services defined by this specification allow objects to be bound to lifecycle contexts, to be injected, to be associated with interceptors and decorators and to interact in a loosely coupled fashion by initiating and observing events. Primary objective of introducing CDI is to bring together different types of beans available in Java EE Platform like JSF Managed Beans, Enterprise Java Beans, etc. CDI helps in defining "bean" object which can be used in any of the tiers of the Java EE platform. In simple terms, a bean object defines applications state or logic within a context. Any Java EE component can be considered as a bean provided the life cycle of the component is managed by the container. Bean in CDI like any other beans is a POJO and the beauty is that it can take the shape of any other component with the help of annotations. Any class can be used as a managed-bean provided it meets all the requirements of a bean. Annotations are used to mark the bean to be of a specific type, @Model annotation is used on the bean to mark the class as a Model in the MVC architecture, @Named annotation is used on the bean to mark the class as a Managed Bean in the Java Server Faces application, @Stateful annotation can be added to mark the class as a Stateful Enterprise Bean.

@Model
public class Login {
private String uname;
private String pwd;

public void setUname(String uname) {
this.uname = uname;
}
public String getUname() {
return this.uname;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getPwd() {
return this.pwd;
}
}

@Named
@SessionScoped
public class LoginBean implements Serializable {
…
}

@Model
@Stateful
@SessionScoped
public class Login {
….
}

Apart from these annotations there are qualifier annotations and scoped annotations. CDI services are provided to the components through the transaction and security annotations.

Qualifier Annotations

A qualifier helps in identifying a specific implementation of Java Class or Interface to be injected. A qualifier is an annotation applied to a bean. In order to define the qualifier annotation, the type should be defined as a qualifier: @Qualifier. A qualifier type is a Java annotation, custom annotation defined using @Target({METHOD, FIELD, PARAMETER, TYPE}) and @Retention(RUNTIME) annotations.

@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface ValidUser {
…
}

The qualifier defined in the above example can be used as follows:

….
@ValidUser
@Inject
private String username;
….

Scoped Annotations

Scope is an important factor in Web applications. Scope defines the state of the object that is being held by the bean in a Web application. While the Web applications have well defined scope, there is no well-defined scope for the enterprise beans. When enterprise bean components are used within the Web applications, the components are not aware of the contexts of the Web applications and have no state associated with those contexts. It is meaningless to add the scope of the enterprise bean to the web-tier context. The following are the scopes used in the CDI beans:

  • Request Scope – @RequestScoped – Single HTTP request (Defined in Servlet Specification)
  • Session Scope – @SessionScoped – Across HTTP requests – a single user sequential requests (Defined in Servlet Specification)
  • Application Scope – @ApplicationScoped – Across all users within an application (Defined in Servlet Specification)
  • Dependent Scope – @Dependent – For a Single client and the lifecycle is same as that the client (Defined in CDI Specification)
  • Conversation Scope – @ConversationScoped – Scope between multiple invocations of JSF life cycle within the session boundaries (Defined in CDI Specification)

CDI objects are scoped, have a well defined lifecycle context in the Java EE container. They are automatically created and destroyed when the context in which they created ends.


Tags: Java EE 6, EJB, CDI

Originally published on http://www.developer.com.

Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel