4 Replies Latest reply on Aug 28, 2010 11:09 AM by robshep

    extends injection @IN

    mireksz.m.szajowski.streamsoft.pl

      Hello,
      I have


      @In SpecialInterface object



      SpecialInterface  - is not EJB or Seam bean. This is special kind of bean which I creates dynamicly. I need some way to tell seam
      'If you don't have bean to inject ask me' some listener which is invoking when seam can't resolve dependency . For example




      public Object resolveDependency(someInfoAboutFIeld){}
      





      Best regards
      Mirek


        • 1. Re: extends injection @IN
          robshep

          'If you don't have bean to inject ask me'


          This is the factory pattern!


          Provide a @Factory("object") method in another SEAM component which provides these types of objects.


          You've also got @Unwrap if the manager pattern is a better fit for your bean


          Good luck


          Rob

          • 2. Re: extends injection @IN
            mireksz.m.szajowski.streamsoft.pl

            But @Factory has to have name of the bean. I need @Factory(SpecialInterface.class) or better @Factory(AbstractSpecialInterface.class)

            • 3. Re: extends injection @IN
              robshep

              No problem,


              Seam components are those that are @Named and can contain @Factorys (for example) - but the beans that are in/out-jected by seam (context varialbles) don't necessarily need to be @Name d components, they can be any arbitrary type, you can even put a type like:


              List<String> myNicknames



              into a context as a context variable.


              So,


              1. Here, for example is your business Interface.


              public interface SpecialInterface 
              {
                   public String somethingSpecial();
              }




              2. but these are made dynamically (depending on some other context variables maybe....) so let's make a @Factory in it's own SEAM component


              @Name("specialInterfaceFactory")
              public class FactoryTest 
              {
                   @Factory(autoCreate=true, value="mySpecialInterface")
                   public SpecialInterface getSpecialInterface()
                   {
                        return new SpecialInterface(){
              
                             @Override
                             public String somethingSpecial() {
                                  return "Seem Special?";
                             }
                        };
                   }
              }



              3. Then when we want an instance of this Interface we can inject it, (here we use a different name than the member field)


              @Name("specialInterfaceUser")
              @Install(dependencies="specialInterfaceFactory")
              public class SpecialInterfaceUser 
              {
                   @In("mySpecialInterface") SpecialInterface specialInterface;
                   @Logger Log log;
                   
                   @Observer("org.jboss.seam.postInitialization")
                   public void doSomething()
                   {
                        log.warn(specialInterface.somethingSpecial());
                   }
              }



              Note, I just added the observer so I didn't need to write a view - and so also made sure the installation dependencies were OK (may be strictly be necessary in this case


              Et voila:

              15:45:29,483 INFO  [STDOUT] WARN  - SpecialInterfaceUser       - Seem Special?



              Hope this helps


              Rob

              • 4. Re: extends injection @IN
                robshep

                Minor typo: May not be strictly necessary in this case.