@Stateless
public class Waiter {
@Inject
private Soup soup;
@EJB
private Chef chef;
public String orderSoup(String name){
soup.setName(name);
return chef.prepareSoup().getName();
}
}
CDI @RequestScoped
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
beans that inject it throughout a same request.
Example
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.
Waiter
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.
Soup
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.
@RequestScoped
public class Soup {
private String name = "Soup of the day";
@PostConstruct
public void afterCreate() {
System.out.println("Soup created");
}
public String getName() {
return name;
}
public void setName(String name){
this.name = name;
}
}
Chef
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.
@Stateless
public class Chef {
@Inject
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;
@EJB
private Waiter joe;
@Before
public void startContainer() throws Exception {
container = EJBContainer.createEJBContainer();
container.getContext().bind("inject", this);
}
@Test
public void orderSoup(){
String soup = joe.orderSoup(TOMATO_SOUP);
assertEquals(TOMATO_SOUP, soup);
soup = joe.orderSoup(POTATO_SOUP);
assertEquals(POTATO_SOUP, soup);
}
@After
public void closeContainer() throws Exception {
container.close();
}
}
Running
The test calls orderSoup() twice, once with "Tomato Soup" and once with "Potato Soup". In both
calls the Chef gets the soup the Waiter named, because both beans share the same Soup
instance while the call is running.
Both calls come from the test thread, which is not an HTTP request. For such local EJB calls the
container keeps one request context for the thread, so the Soup bean is created only once and
the output shows a single Soup created. In a web application, each HTTP request gets its own
request context and therefore its own Soup instance.
[INFO] Running org.superbiz.cdi.requestscope.RestaurantTest
...
INFO: Deployed Application(path=.../examples/cdi-request-scope)
Soup created
INFO: Undeploying app: .../examples/cdi-request-scope
...
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0