3 Replies Latest reply on Jul 15, 2009 5:01 PM by Nikos Paraskevopoulos

    How to Dynamicly inject a bean

    Etai Arnon Newbie

      Lets say a have a superclass A, and classes B and C extend A.


      I want to use the same SLSB, to handle classes B and C (e.g. read/save to db)
      Each class has its own JSF screen, but basically i'll have to do exactly the same functionality in the BL.


      I want to somehow inject(and outject) to the SLSB the correct instance of the active class (e.g if a user is in the screen representing A, the instance of A will be injected).
      Whats the right approach for this? 

        • 1. Re: How to Dynamicly inject a bean
          Nikos Paraskevopoulos Novice

          You could use the factory pattern. It is implemented in SEAM by annotating a method with @Factory. Your method will somehow sense the screen you are currently on (there are many ways to do this) and return the appropriate subclass of A. Take care to place the returned value in the correct context!


          Read more in the section entitled Factory and manager components in the documentation.

          • 2. Re: How to Dynamicly inject a bean
            Etai Arnon Newbie

            Thank you Nikos, looks like just the thing i was looking for,


            anyway about the:



            Nikos Paraskevopoulos wrote on Jul 15, 2009 11:20:


            Your method will somehow sense the screen you are currently on (there are many ways to do this)


            I'm sure there are many ways, but i'm kind of new in the JEE/SEAM programming world, so could please give me a hint about what's a good way to do this?,
            thanks again.

            • 3. Re: How to Dynamicly inject a bean
              Nikos Paraskevopoulos Novice

              Hello again,


              First thing that comes to mind is using the view id: FacesContext.getCurrentInstance().getViewRoot().getViewId(). That is pretty simple but dirty, since you need to specify the view id explicitly.


              A more JSF-ish way is this: Create the A implementation (instance of B or C) from the action method that navigates to pageB or pageC. Then set it into the component that contains the factory method. E.g.:


              // component that contains the factory method
              
              @Name("factoryComp")
              public class FactoryComp
              {
                private A alphaImpl;
              
                @Factory("alphaImpl")
                public A getAlphaImpl() { return alphaImpl; }
              
                public void setAlphaImpl(A a) { alphaImpl = a; }
              }
              



              // component containing action method
              
              @Name("actionComp")
              public class ActionComp
              {
                @In FactoryComp factoryComp;
              
                public String actionMethod() {
                  A alphaImpl;
                  String result = null;
                  if( ...conditions... ) {
                    alphaImpl = new B();
                    result = "pageB";
                  }
                  else if( ...conditions... ) {
                    alphaImpl = new C();
                    result = "pageC";
                  }
                  factoryComp.setAlphaImpl(alphaImpl);
                  return result;
                }
              }
              



              Finally you can use a page action, different for pages B and C. The page action of pageB will create a new B() and set it into the factory. Same for action of pageC.