What is the difference of javax.enterprise.context.SessionScoped
and javax.faces.bean.SessionScoped
?
Does anyone know how to explain?
What is the difference of javax.enterprise.context.SessionScoped
and javax.faces.bean.SessionScoped
?
Does anyone know how to explain?
The javax.faces.bean.SessionScoped
annotation must be added to% s of JSF% s to denote that the bean has a session scope. That is, the bean instance is kept in session.
For example, suppose you have:
@javax.faces.bean.ManagedBean
@javax.faces.bean.SessionScoped
public class MeuBean implements java.io.Serializable {
// Um monte de métodos.
}
And then on several pages of your (X) HTML, you have a ManagedBean
expression. This meuBean
instance will be stored in the session. Each request of the same client will bring the same instance with the same data and when changing one, the change will reflect in all the requests of that client. However different clients will have different instances of this bean.
meuBean
is a CDI annotation to be placed on attributes and methods to denote that the attribute or method in question stores or produces some feature that is the same in each session. Usually used in factory classes along with the javax.enterprise.context.SessionScoped
annotation. This is so that when injecting dependencies with @Produces
, when the same resource is injected into multiple places, so that the factory method is not invoked for each of those locations. It will be invoked only the first time and the result will be stored in the session, to be reused in each injection to be performed in that session.
Example that I took from Caelum's booklet that I wrote below:
public class ProdutorEntityManager {
@Produces @RequestScoped
public EntityManager criaEntityManager() {
// ...
}
}
References: