5 Replies Latest reply on Sep 3, 2010 7:12 AM by nimo22

    scope widening of no scoped beans

    nimo22

      I have this:


      @SessionScoped @Named
      public class MyBean implements Serializable{ 
      
      private static final long serialVersionUID = ll;
      
      
      private @Inject Users u;
      
      }




      where Users is a no-scoped object!



      public class User implements Serializable{ 
      
      ..}



      By injecting u, I get a NEW INSTANCE of Users - u instance inherits a scope (dependent-scope). Does it mean, that u is sessionscoped? Have I widened the scope of u from no-scope to dependent(sessionscope) ?



      Would it be better to avoid to inject no-scoped-beans:


      @SessionScoped @Named
      public class MyBean implements Serializable{ 
      
      private static final long serialVersionUID = 1l;
      
      
      private Users u;
      // getter/setter
      
      }





      If I create/assign the instance u within MyBean without injecting it, then u will also live within a sessionscope.


      What is best practice?






        • 1. Re: scope widening of no scoped beans
          nimo22

          And what is the difference of this



          @Inject @New Users u;




          and that


          @Inject Users u;




          when Users is still a no-scoped-bean


          public class User implements Serializable{ 
          
          ..}
          







          • 2. Re: scope widening of no scoped beans
            asiandub

            There is nothing like a no-scoped bean, so I'm a bit confused what you mean. The distinction is between managed beans and the rest, and between normal scoped and pseudo scoped. But no-scoped has no meaning in CDI...



            @SessionScoped @Named
            public class MyBean implements Serializable{ 
            
            private @Inject Users u;
            
            }



            As you say yourself, the user will be dependent scoped.






            If I create/assign the instance u within MyBean without injecting it, then u will also live within a sessionscope.

            That's absolutely true. But then you are taking care of the dependencies, and this will make all Weld developers sad and redundant...




            @Inject @New Users u;



            This makes sense if (and only if) the User is a normal-scoped (not pseudo-scoped) bean.  Compare the spec:




            This allows the application to obtain a new instance of a bean which is not bound to the declared scope, but has had dependency injection performed.




            • 3. Re: scope widening of no scoped beans
              nickarls

              You can have something like


              @SessionScoped
              public class LoginBean {
                @Produces @Named User user = ...
              }
              



              which makes sessionscoped snapshots with a dependent scoped producer. Of course, you'll have to be careful with scope lifecycle-lengths when cross-injecting like that. So @Produces @SessionScoped @Named is safer for that, I think.

              • 4. Re: scope widening of no scoped beans
                nimo22

                With no-scoped, I mean beans which has no scope declared: (maybe compare it to java.faces.bean.NonScoped).



                // no-scoped=pseudo-scoped: there is no scope defined in User-Object
                public class User implements Serializable{ 
                
                ..}




                @Inject @New Users u;


                I know, for normal-scoped beans @New makes sense.


                Why is it senseless for pseudo-scoped bean?


                Because @Inject will inject everytime a new instance from a pseudo-scoped bean. Am I right? So @New is redundant for injected pseudo-scoped beans. Am I right?

                • 5. Re: scope widening of no scoped beans
                  nimo22



                  So @Produces @SessionScoped @Named is safer for that, I think.

                  That is indeed the way, I did it!