5 Replies Latest reply on Oct 14, 2007 4:56 AM by adamw

    Injecting a narrower-scoped bean into a wider-scoped bean

    adamw

      Hello,

      I looked through the manual, but couldn't find an answer.

      Let's say I have beans with following scopes:
      * A - application
      * S - stateless
      * C - conversation

      In the examples I have often seen C-beans are injected into S-beans. As I understand, this is correct wrt to concurrent requests, that is, Seam will create enough copies of S-beans so that an S-bean serves at most one request at a time (if that wasn't true, and two requests invoked a method on an S-bean at the same time, one injected value would overwrite the other...)?

      Creating additional beans isn't possible with A-beans however: Seam can create at most one A-bean. So I suppose injecting a C-bean into an A-bean should produce an error, or at least a warning, right?

      --
      Regards,
      Adam

        • 1. Re: Injecting a narrower-scoped bean into a wider-scoped bea
          adamw

          A similar problem, of Event-beans injected into Session-beans is solved by the concurrency model (http://docs.jboss.com/seam/2.0.0.CR2/reference/en/html_single/index.html#concurrency), which serializes requests to session beans. So it seems that injecting C-beans/E-beans into A-beans is a programmer error.

          But what about stateless? Does Seam take care that enough copies are created?

          --
          Adam

          • 2. Re: Injecting a narrower-scoped bean into a wider-scoped bea
            pmuir

             

            "adamw" wrote:
            Let's say I have beans with following scopes:
            * A - application
            * S - stateless
            * C - conversation

            In the examples I have often seen C-beans are injected into S-beans. As I understand, this is correct wrt to concurrent requests, that is, Seam will create enough copies of S-beans so that an S-bean serves at most one request at a time (if that wasn't true, and two requests invoked a method on an S-bean at the same time, one injected value would overwrite the other...)?


            A you say, you can't invoke two methods on the same SLSB at the same time, it's stateless (this is part of the EJB3 contract, not Seam, to be precise).

            Creating additional beans isn't possible with A-beans however: Seam can create at most one A-bean. So I suppose injecting a C-bean into an A-bean should produce an error, or at least a warning, right?


            Yes, this would be a bad thing to do (you should inject the application scoped component into the conversation scoped component). Add a FAQ to SeamProblemsFAQ if you like?

            • 3. Re: Injecting a narrower-scoped bean into a wider-scoped bea
              matt.drees

               

              "adamw" wrote:
              So it seems that injecting C-beans/E-beans into A-beans is a programmer error.


              I imagine this would be fine if you mark your A-bean @Synchronized. (Though of course, this could cause a bottleneck.) Haven't yet tried it though.

              • 4. Re: Injecting a narrower-scoped bean into a wider-scoped bea
                gavin.king

                 

                "matt.drees" wrote:
                "adamw" wrote:
                So it seems that injecting C-beans/E-beans into A-beans is a programmer error.


                I imagine this would be fine if you mark your A-bean @Synchronized. (Though of course, this could cause a bottleneck.) Haven't yet tried it though.


                That is correct.

                • 5. Re: Injecting a narrower-scoped bean into a wider-scoped bea
                  adamw

                   

                  "pete.muir@jboss.org" wrote:
                  Add a FAQ to SeamProblemsFAQ if you like?


                  Added, thanks for the info!

                  Adam