6 Replies Latest reply on Oct 24, 2011 7:55 AM by Thyago Schleuss

    Component.getInstance() vs. injection

    Arbi Sookazian Master

      So we know that all injected properties of a component are dynamically injected prior to business method execution.  This means every time a business method is invoked whether or not those properties need to be re-freshed all of the properties marked @In are re-fresh/re-injected.  So let's take a hypothetical case.  Say we have 20 Seam components that need to be injected into component Foo.


      @Name("foo")
      public class Foo {
          
         @In
         private C1 c1;
      
         
         @In
         private C2 c2;
      
         ...
      
         
         @In
         private C20 c20;
      
         @Begin(join=true)
         public void begin(){
         ...
         }
      
         @End
         public void end(){
         ...
         }
      
         @Destroy
         public void destroy(){...}
      }



      Would it not be more efficient to use Component.getInstance(C1.class) instead of the unnecessarily repetitive and time-consuming swarm of dynamic injections?  For a particular business method, you get the component you need at that exact time.  Instead of getting all 20 of them when you may only need one of them for that particular business method.


      It's kind of like you're a rich person with 20 chauffeurs and every time you step out of your house all 20 of your cars are on and running but you only drive away at that time with one of them.  So why turn on all 20 cars every time you step out of your house?  That's overkill.


      Just wondering if anybody has noticed this and it not actually using @In except when absolutely necessary like injecting SMPC and Log instance via @Logger.


      Out of curiosity, I tried this:


      public void foo(){
           ManagedPersistenceContext smpc = (ManagedPersistenceContext)Component.getInstance(ManagedPersistenceContext.class);
                try {
                     entityManager = smpc.getEntityManager();
                }
                catch (SystemException e){
                     //no op
                }
      }



      and I got:



      java.lang.IllegalArgumentException: No @Name annotation for class: org.jboss.seam.persistence.ManagedPersistenceContext