package injection; import java.io.Serializable; import javax.enterprise.context.Conversation; import javax.enterprise.context.ConversationScoped; import javax.inject.Inject; import javax.inject.Named; @Named(value = "conversationData") @ConversationScoped public class ConversationData implements Serializable { @Inject private ApplicationData applicationData; @Inject private Conversation conversation; private int counter; private boolean elevated; int beanID; private boolean counted; public ConversationData() { counter = 0; elevated = false; } public void increment() { applicationData.increment(); counter++; } public int getCounter() { return counter; } public int getBeanID() { if (!counted) { beanID = applicationData.getBeanID(); } counted = true; return beanID; } public boolean isElevated() { return elevated; } public String beginConversation() { conversation.begin(); elevated = true; return "index"; } public String endConversation() { conversation.end(); elevated = false; return "index"; } }
This class defines conversation scoped beans. Conversation scoped beans start out behaving like request scoped beans. As such, new beans are created for each request. One of these beans can be elevated to conversation scope by sending a begin() message to an injected conversation variable. It can be returned to request scope by sending an end() message to the conversation variable.
The ConversationData class, like the ApplicationData class, defines a counter property with an increment action method. In addition to incrementing its own counter property, the ConversationData increment method invokes the ApplicationData increment method through an injected applicationData bean.