4 Replies Latest reply on Sep 1, 2010 9:12 AM by pmuir

    javax.faces.bean.SessionScoped vs javax.enterprise.context.SessionScoped

    nimo22

      Where lies the difference between this kind of SessionScope


      javax.faces.bean.SessionScoped;




      and that kind of SessionScope:



      import javax.enterprise.context.SessionScoped;




      As Session and Application Beans should be EJBs, should I import javax.enterprise.context.SessionScoped instead of javax.faces.bean.SessionScoped ?



      Regarding to Weld Documentation (2.2.2. Session beans:



      Many beans (including any @SessionScoped or @ApplicationScoped beans) are available for concurrent
      access. Therefore, the concurrency management provided by EJB 3.1 is especially useful. Most session and
      application scoped beans should be EJBs.
        • 1. Re: javax.faces.bean.SessionScoped vs javax.enterprise.context.SessionScoped
          nickarls

          Use the CDI versions wherever available. Yes, the duplication is confusing.


          Didn't know @SessionScoped EJBs could be concurrently accessed in EJB 3.1. Is the @Lock for them also, I got the picture it was a Singleton feature?

          • 2. Re: javax.faces.bean.SessionScoped vs javax.enterprise.context.SessionScoped
            nimo22

            When using ConversationScoped, I have to use this:





            import javax.enterprise.context.ConversationScoped;




            as this



            import javax.faces.bean.ConversationScoped;




            does not exist.



            This is not consistency.


            So are there any good reasons not to use this


            import javax.enterprise.context.SessionScoped;



            instead of


            import javax.faces.bean.SessionScoped;





            I guess, I should use this


            import javax.enterprise.context.SessionScoped;


            and follow this advise:



            Most session and application scoped beans should be EJBs.



            • 3. Re: javax.faces.bean.SessionScoped vs javax.enterprise.context.SessionScoped
              asiandub

              some more background:



              Where lies the difference between this kind of SessionScope
              javax.faces.bean.SessionScoped;
              and that kind of SessionScope:
              import javax.enterprise.context.SessionScoped;


              Both annotations origin from different APIs, and have been specified (more or less) independently. While the JSF session annotation has a clear focus on the JSF view technology, CDI requires a session annotation which is present, no matter if JSF is deployed or not (CDI can be used without JSF). So CDI had no choice but defining it's own session scope.



              When using ConversationScoped, I have to use this:
              import javax.enterprise.context.ConversationScoped;
              as this
              import javax.faces.bean.ConversationScoped;
              does not exist.
              This is not consistency.

              Here it's a bit different. Seam established the concept of conversations, which then CDI adopted. In parallel, JSF 2.0 was specified as JSR 314, with a long discussion whether conversation scope was worthwhile or not. Finally it was agreed on a compromise (Flash- and View-Scope), but a Conversation scope was not specified.


              Another confusing example is the term managed bean, which is used by both sides, JSF and CDI, but with completely different meanings.


              The rule is - as Nicklas pointed out - use CDI scopes wherever possible. You are using CDI after all, so you want your beans to be in control of the CDI container. :-)


              BTW (1): Seam 3 offers integration with the JSF View-Scope in a way that you can use this scope in a completely transparent manner.


              BTW (2): Found this code in seam-faces, it explains a lot:



              /**
               * Alias the JSF scope annotations to the CDI scope annotations. If a JSF scope
               * annotation is detected, advise the developer to update the code to use the
               * equivalent CDI scope annotation. Forbid the developer from using the JSF
               * managed bean annotation.
               * 
               * @author Dan Allen
               */
              public class FacesAnnotationsAdapterExtension implements Extension
              {
                 private final Map<Class<? extends Annotation>, Class<? extends Annotation>> scopeAliasMapping;
              
                 /*
                  * For unit testing
                  */
                 static final Map<Class<?>, Class<? extends Annotation>> aliasedBeans = new HashMap<Class<?>, Class<? extends Annotation>>();
              
                 public FacesAnnotationsAdapterExtension()
                 {
                    scopeAliasMapping = new HashMap<Class<? extends Annotation>, Class<? extends Annotation>>(3);
                    scopeAliasMapping.put(javax.faces.bean.RequestScoped.class, javax.enterprise.context.RequestScoped.class);
                    scopeAliasMapping.put(javax.faces.bean.SessionScoped.class, javax.enterprise.context.SessionScoped.class);
                    scopeAliasMapping.put(javax.faces.bean.ApplicationScoped.class, javax.enterprise.context.ApplicationScoped.class);
                 }
              
                 public void aliasJsfScopeIfDetected(@Observes final ProcessAnnotatedType<Object> annotatedType)
                 {
                    for (Class<? extends Annotation> scope : scopeAliasMapping.keySet())
                    {
                       if (annotatedType.getAnnotatedType().isAnnotationPresent(scope))
                       {
                          System.out.println("WARNING: Please annotate class " + annotatedType.getAnnotatedType().getJavaClass() + " with @" + scopeAliasMapping.get(scope).getName() + " instead of @" + scope.getName());
                          aliasedBeans.put(annotatedType.getAnnotatedType().getJavaClass(), scope);
                          annotatedType.setAnnotatedType(decorateType(annotatedType.getAnnotatedType(), scope));
                          break;
                       }
                    }
                 }
              
                 public void failIfJsfManagedBeanAnnotationPresent(@Observes final ProcessBean<?> bean)
                 {
                    if (bean.getAnnotated().isAnnotationPresent(javax.faces.bean.ManagedBean.class))
                    {
                       bean.addDefinitionError(new RuntimeException("Use of @javax.faces.bean.ManagedBean is forbidden. Please use @javax.inject.Named instead."));
                    }
                 }
              
              ...
              





              • 4. Re: javax.faces.bean.SessionScoped vs javax.enterprise.context.SessionScoped
                pmuir

                I made this into a couple of FAQs - wiki://137342 and wiki://137343.