Context Dependency Injection with JBoss Weld

weldContexts and Dependency Injection (CDI) (JSR 299) defines a set of services for the Java EE environment that makes applications much easier to develop. It provides an architecture that allows Java EE components such as servlets, enterprise beans, and JavaBeans to exist within the lifecycle of an application with well-defined scopes

 

 

 

In short, CDI helps to bridge a known "gap" between the enterprise tier and the web tier. Until now, if you were to access your business logic from your JSF pages, the best strategy would be to access the JSF Managed Beans which in turn contacted the enterprise components.
jsf-ejb

With CDI the JSF page can access directly enterprise components bringing transactional support to the web tier.

jsf-cdi
How does the magic trick happens ? This can happen thanks to the definition of Managed beans.
Managed beans, which is introduced in Java EE 6, is designed to unify all of the various types of beans in Java EE, including JSF managed beans, enterprise beans, and CDI beans.

Let's understand this better with a sample. We will create a demo application which uses a Facelet to access directly a Session Bean (See this Facelets tutorial for info about facelets)

This sample page collects input (bound to the EL expression #{user.name}) and has an action. The action invokes directly an EJB method:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:f="http://java.sun.com/jsf/core">
 
 <h:body>
 <h:form>
 <h:outputText value="Enter your name:" />
 <h:inputText id="name" value="#{user.name}"/> 

 <h:commandButton id="action" value="Done" action="#{clock.action}"  /> 
 <h:messages/>
</h:form>  
 </h:body>
 </html>

This is the sample EJB:
package sample;

import javax.ejb.Stateless;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.Date;

@Named
@Stateless
public class Clock {
 
 @Inject User user;
 
 public void action() {
 
 String message = user.getName() + " it is now " + new Date().toString();    
 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(message));
 }

}

The @Named annotation is a Dependency Injection For Java annotation that is used to associate a name with the bean. Because there is no name specified as an argument to the annotation, the name of the bean will be the name of the JavaBean with its first letter made lowercase, that is, clock. The annotation enables the application to reference the bean by that name using the EL expressions in the view.

The @Inject annotation in  is a CDI annotation that is used to identify a dependency injection point, that is, a point at which a dependency on a Java class or interface can be injected. In this case, the annotation identifies a dependency injection point for the user field of the User class.

And here's the User class:
package sample;

import java.io.Serializable;

import javax.enterprise.context.*;
import javax.inject.Named;


@Named
@SessionScoped
public class User  implements Serializable {
 private String name;

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }
}

Here also, the @Named annotation associate the User class with its name (lowercased).
What you should note is the @SessionScoped annotation declares that this bean is a session scoped bean, meaning that its lifecycle is the lifecycle of the session. (And as such, it needs to be Serializable to save its state).