This example show the use of @RequestScoped annotation for injected objects. An object which is defined as @RequestScoped is created once for every request and is shared by all the bean that inject it throughout a request.


This example depicts a similar scenario to cdi-application-scope. A restaurant guest orders a soup from the waiter. The order is passed to the chef who prepares it and passes it back the waiter who in turn delivers it to the guest.


The Waiter session bean receives a request from the test class via the orderSoup() method. A Soup insance will be created in this method and will be shared throughout the request with the Chef bean. The method passes the request to the Chef bean. It then returns the name of the soup to the test class.

public class Waiter {

    private Soup soup;

    private Chef chef;

    public String orderSoup(String name){
        return chef.prepareSoup().getName();


The Soup class is an injectable POJO, defined as @RequestScoped. This means that an instance will be created only once for every request and will be shared by all the beans injecting it.

public class Soup {

    private String name = "Soup of the day";

    public void afterCreate() {
        System.out.println("Soup created");

    public String getName() {
        return name;

    public void setName(String name){ = name;


The Chef class is a simple session bean with an injected Soup field. Normally, the soup parameter would be passed as a prepareSoup() argument, but for the need of this example it's passed by the request context.

public class Chef {

    private Soup soup;

    public Soup prepareSoup() {
        return soup;

Test Case

This is the entry class for this example.

public class RestaurantTest {

    private static String TOMATO_SOUP = "Tomato Soup";
    private EJBContainer container;

    private Waiter joe;

    public void startContainer() throws Exception {
        container = EJBContainer.createEJBContainer();
        container.getContext().bind("inject", this);

    public void orderSoup(){
        String soup = joe.orderSoup(TOMATO_SOUP);
        assertEquals(TOMATO_SOUP, soup);
        soup = joe.orderSoup(POTATO_SOUP);
        assertEquals(POTATO_SOUP, soup);

    public void closeContainer() throws Exception {


In the output you can see that there were two Soup instances created - one for each request.

 T E S T S
Running org.superbiz.cdi.requestscope.RestaurantTest
Apache OpenEJB 4.0.0-beta-2-SNAPSHOT    build: 20111224-11:09
INFO - openejb.home = C:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-request-scope
INFO - openejb.base = C:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-request-scope
INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Found EjbModule in classpath: c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-request-scope\target\classes
INFO - Beginning load: c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-request-scope\target\classes
INFO - Configuring enterprise application: c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-request-scope
INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container)
INFO - Auto-creating a container for bean cdi-request-scope.Comp: Container(type=MANAGED, id=Default Managed Container)
INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)
INFO - Auto-creating a container for bean Chef: Container(type=STATELESS, id=Default Stateless Container)
INFO - Enterprise application "c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-request-scope" loaded.
INFO - Assembling app: c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-request-scope
INFO - Jndi(name="java:global/cdi-request-scope/Chef!org.superbiz.cdi.requestscope.Chef")
INFO - Jndi(name="java:global/cdi-request-scope/Chef")
INFO - Jndi(name="java:global/cdi-request-scope/Waiter!org.superbiz.cdi.requestscope.Waiter")
INFO - Jndi(name="java:global/cdi-request-scope/Waiter")
INFO - Created Ejb(deployment-id=Chef, ejb-name=Chef, container=Default Stateless Container)
INFO - Created Ejb(deployment-id=Waiter, ejb-name=Waiter, container=Default Stateless Container)
INFO - Started Ejb(deployment-id=Chef, ejb-name=Chef, container=Default Stateless Container)
INFO - Started Ejb(deployment-id=Waiter, ejb-name=Waiter, container=Default Stateless Container)
INFO - Deployed Application(path=c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-request-scope)
Soup created
Soup created
INFO - Undeploying app: c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-request-scope
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.412 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

APIs Used


svn co
cd cdi-request-scope
mvn clean install