4 Replies Latest reply on Jun 8, 2009 12:34 PM by khennig

    How to get access to injected components from inner classes?

    khennig

      Hello, why do I have no access to injected components from an inner class? The variables (e.g. a reference to a stateless session bean annotated with @In(create=true)) are always null if accessed from an inner class of a seam component.


      Lets say, I have defined a wrapper class for an entity in my backing bean to provide easy access to additional entity features like detailed information in the view (e.g. fetched via a separate query with JOIN FETCH on relations). To get detailed information I need access to the facade from within the wrapper (wrappers are build from a list of entities returned from the facade, just to complete the picture):



      @Name("backing")
      @Scope(ScopeType.SESSION)
      public class Index implements Serializable {
      
       @In(create = true)
       private FacadeLocal facade;
       ...
        public class EntityWrapper {
      
         private Entity entity
      
         public EntityWrapper(Entity entity) {
          this. entity = entity;
         }
      
         public Entity getDetails() {
          // this will fail because facade is null
          return facade.getEntityDetails(entity.getId());
         }
         ...
        }
      }




      I migrated to Seam. Before I did so, the above pattern worked fine.


      Best regards, Kai



      FACTS: Seam 2.1.0.SP1, Glassfish 9.1, Java 1.5.0, Mac OS X 10.5.5

        • 1. Re: How to get access to injected components from inner classes?
          joblini

          Hi Kai,


          Try calling Component.getInstance(Facade,true) from your inner class.

          • 2. Re: How to get access to injected components from inner classes?
            khennig

            Hi Ingo, ... Component.getInstance(Facade,true) works just fine, thank you that.


            But it looks like a workaround to me, e.g. if a backing bean method depends on the injected facade it can not be called from within the wrapper (at least not without the workaround, wich clutters the code in a way). Calling backing bean methods this way happens quite often in my application, e.g. when implementing the RichFaces ExtendedDatamodel as inner class. It gets filter criteria via backing bean getters, those getters load default values via facades.


            Any idear why the injected component is null?

            • 3. Re: How to get access to injected components from inner classes?
              joblini

              As I understand it the injected component is null because it is in the outer class, which is a separate class from the inner class.  It is helpful to understand that Seam performs injection by intercepting method calls.  Since no method on the outer class is being called, Seam does not intercept the call and inject the value

              • 4. Re: How to get access to injected components from inner classes?
                khennig

                Just finishing this topic.


                Ingo is right. No bijection takes place if a class instance of a Seam component is used outside of a Seam context. Furthermore are injected values disinjected (i.e. set to null) immediately after method completion as stated in the Seam reference (chapter 4.3 Bijection, Version 2.1.1.GA).