2 Replies Latest reply on Jul 7, 2011 2:19 PM by Zak S

    Scope of @Producer produced beans

    Ro Chap Newbie

      Hi,


      In chapter 3 of the Weld reference documentation, there's a Login class defined like:



      @SessionScoped
      @Named
      public class Login implements Serializable {
      
       // ...
      
       @Produces @LoggedIn User getCurrentUser() {
        return user;
       }
      
      }



      I am new to Seam and Weld so sorry if my question is kind of dumb. But what would be the scope of the User instance returned from the getCurrentUser() producer method? Would it be identical to the scope of the Login class? Can it be narrowed down to be request scoped? E.g.:


      @SessionScoped
      @Named
      public class Login implements Serializable {
      
       // ...
      
       @Produces @LoggedIn @RequestScoped User getCurrentUser() {
        return user;
       }
      
      }



      Thanks in advance.

        • 1. Re: Scope of @Producer produced beans
          Robert  Anderson Newbie
          Hi,

          "The scope of the producer method defaults to @Dependent, and so it will be called every time the container injects this field or any other field that resolves to the same producer method." (http://docs.jboss.org/weld/reference/1.0.1-Final/en-US/html_single/#d0e3514)

          Best regards,

          Robert
          • 2. Re: Scope of @Producer produced beans
            Zak S Newbie
            <blockquote>
            _Ro Chap wrote on Jul 07, 2011 06:20:_<br/>

            Hi,

            In chapter 3 of the Weld reference documentation, there's a Login class defined like:


            `@SessionScoped
            @Named
            public class Login implements Serializable {

            // ...

            @Produces @LoggedIn User getCurrentUser() {
              return user;
            }

            }`

            I am new to Seam and Weld so sorry if my question is kind of dumb. But what would be the scope of the User instance returned from the |getCurrentUser()| producer method? Would it be identical to the scope of the |Login| class? Can it be narrowed down to be request scoped? E.g.:

            `@SessionScoped
            @Named
            public class Login implements Serializable {

            // ...

            @Produces @LoggedIn @RequestScoped User getCurrentUser() {
              return user;
            }

            }`

            Thanks in advance.
            </blockquote>

            If 'user' is a field of Login class then it is session scoped, while the producer method remains @Dependent. This means that the producer method will be called any time a @LoggedIn User bean is required but the same 'user' instance will be returned as long as you're in the same session.

            If you want to narrow it down to request scope (i.e. a new user per request) then the following code would do it



            @SessionScoped
            @Named
            public class Login implements Serializable {

            // ...

            @Produces @LoggedIn @RequestScoped User getCurrentUser(@New User newUser) {

              return newUser;
            }

            }

            The above producer receives a new @Dependent User bean and returns it, which has the effect of promoting it to @RequestScoped. This is preferred to creating a new instance  yourself (e.g. User newUser = new User()) since the container initialized user bean's lifecycle methods (@PostConstruct, etc) and interceptors will be called.