3 Replies Latest reply on May 31, 2006 7:52 AM by lcoetzee

    Circular injection problem

    lcoetzee

      Hi,

      I ran across a very interesting problem with circular injection (and is looking for a better way to resolve it).

      I have two stateful session beans (A and B) . In A I do a

      @In(create=true) private B b
      while in B I inject A
      @In(create=true) private A a
      (Please dont ask why ;-)

      My impression is that when I call A, it injects B, which in turns injects A, which in turn injects B etc. until things go slightly wrong.

      I have resolved it by changing the injection of B (in A) to
      @In(create=false, required=false) private B b
      and then later (in the method where I actually need access to B from A) I do a
      b = (B)Component.getInstance("b",true);


      Any suggestions as to try and prevent this circular injection ?

      Thanks

      Louis


        • 1. Re: Circular injection problem
          eekboom

          I fell for that problem, too.
          IMHO the Seam docs should prominently include some advice on application structure/layering to avoid this.

          That's also the only suggestions I can make: Restructure your application and introduce clean layering to avoid any circular or overly complex dependencies. (So I'm afraid I have to ask why you want A and B to have a circular dependency.)

          I think it's best to have a first layer that is accessed by JSF: Components for action handlers and data models. Action handlers can of course have data models or entity beans injected, but not other action handers.
          If needed add some lower layers that can for example handle persistence or business logic.

          But I'd very much like to hear opinions of more experienced Java EE/Seam developers.

          • 2. Re: Circular injection problem
            gavin.king

            So you get a stack overflow?

            If so, then please submit a test case to JIRA.

            • 3. Re: Circular injection problem
              lcoetzee

              Added a test case/sample app in Jira.

              http://jira.jboss.com/jira/browse/JBSEAM-244

              Louis