Java Reference
In-Depth Information
Scope
Annotation
Description
Application
@ApplicationScoped
Application scoped beans live through the
whole application lifetime. Beans in this
scope are shared across user sessions.
Conversation @ConversationScoped
The conversation scope can span multiple
requests, but is typically shorter than the
session scope.
Dependent @Dependent
Dependent scoped beans are not shared, any
time a dependent scoped bean is injected, a
new instance is created.
As we can see, CDI has equivalent scopes to all JSF scopes. Additionally CDI adds
two additional scopes. The first CDI-specific scope is the conversation scope, which
allows us to have a scope that spans across multiple requests, but is shorter than
the session scope. The second CDI-specific scope is the dependent scope, which
is a pseudo scope. A CDI bean in the dependent scope is a dependent object of an-
other object; beans in this scope are instantiated when the object they belong to is
instantiated, and destroyed when the object they belong to is destroyed.
Our application has two CDI named beans. We already discussed the customer
bean, the other CDI named bean in our application is the controller bean.
package com.ensode.controller;
import com.ensode.model.Customer;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
@Named
@RequestScoped
public class Controller {
@Inject
private Customer customer;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public String navigateToConfirmation() {
//In a real application, we would save new customer
// data to database here.
return "confirmation";
} }
Search WWH ::




Custom Search