Advertise with us

Our eBooks

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.

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

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 #{}) and has an action. The action invokes directly an EJB method:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns=""
 <h:outputText value="Enter your name:" />
 <h:inputText id="name" value="#{}"/> 

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

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;

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 javax.enterprise.context.*;
import javax.inject.Named;

public class User  implements Serializable {
 private String name;

 public String getName() {
 return name;

 public void setName(String 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).


© 2021 All Rights Reserved.

Please publish modules in offcanvas position.