Difference of Session in Java

Asked

Viewed 111 times

2

What’s the difference of javax.enterprise.context.SessionScoped and javax.faces.bean.SessionScoped?

Can anyone explain?

  • here is a good answer (in English...) http://stackoverflow.com/questions/15057564/why-are-there-different-bean-management-annotations

1 answer

1


The annotation javax.faces.bean.SessionScoped should be added to ManagedBeanJSF s to denote that the bean has a session scope. That is, that the bean instance is maintained in the session.

For example, let’s assume 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 an expression meuBean. That instance meuBean 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 be reflected in all requests of this client. However different clients will have different instances of this bean.

Already the javax.enterprise.context.SessionScoped is a CDI annotation to be placed in attributes and methods to denote that the attribute or method in question stores or produces some resource that is the same in each session. Usually used in type classes Factory along with the annotation @Produces. This serves to that in the injection of dependencies with the @Inject, when the same resource is to be injected in several locations, so that the Factory is not invoked for each of these 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 I took from the Caelum booklet that I wrote below:

public class ProdutorEntityManager {

   @Produces @RequestScoped
   public EntityManager criaEntityManager() {
       // ...
   }
}

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.