2 Replies Latest reply on Nov 25, 2008 3:44 PM by Guillaume Jeudy

    Polymorphism and SEAM

    Bob Corcoran Newbie

      Hi,


      I have an abstract class that is subclassed by a few stateless EJB classes. From my backing bean, I would like to call a method on one of the subclasses based on the state of a particular field. I'm not sure the best way to accomplish this in SEAM. So far, I have...


      @EJB
      AbstactContentMessages contentMessages;


      I would call a method on a subclass of the AbstactContentMessages class, but I don't know which subclass until runtime (that is, until the form is submitted).


      Would I need to inject all possible subclasses of AbstactContentMessages that I might use?

        • 1. Re: Polymorphism and SEAM
          Tobias Hill Newbie

          Maybe there is some clever way of doing this with the @In feature that I am not aware of. Until someone enlightens you and me I think you should be fine with:



          AbstactContentMessages contentMessages = 
            (AbstactContentMessages) Component.getInstance("nameOfSubComp");
          



          I hope it helps.



          /Tobias

          • 2. Re: Polymorphism and SEAM
            Guillaume Jeudy Master

            If you use


            @In
            AbstractContentMessages contentMessages;
            



            Combined with (can be in a different POJO or EJB class)


            @Factory("contentMessages")
            public AbstractContentMessages create() {
               // return different subclasses here.
            }
            


            Would that work? I've been using @Factory extensively to enable the use of a polymorphic EL expression. You can also try out the more obscure @Unwrap annotation, even though I never used it myself. The main difference between the 2 is @Unwrap is called everytime it is referenced. @Factory is only called if the corresponding EL expression can't be resolved in any contexts.