4 Replies Latest reply on Feb 23, 2010 11:29 PM by dhinojosa

    Programmatic component creation

    cyberoblivion

      I have a seam component that needs to dynamically create a number of other seam components which need injection to work.


      For instance




      @Name("componentCreator")
      @Scope(ScopeType.CONVERSATION)
      public class ComponentCreator{
      @Create
      public void init() {
      for(String name: componentNames){
                  MyComponentClass component = new MyComponentClass();
                  //...somehow register component for injection and management...
              }
      }
      }




      Is there a way to do this with seam?


      Thanks in advance

        • 1. Re: Programmatic component creation
          dhinojosa

          I'll bite...Seems unnecessary to do such a thing considering a component is lazily instantiated when called from a page, i.e.#{myComponent.myProperty}.  What is your goal?

          • 2. Re: Programmatic component creation
            cyberoblivion
            I guess the idea is to dynamically set what #{myComponent} resolves to at runtime based on application configuration data from a database rather than a static components.xml file or annotations on pojos.


            • 3. Re: Programmatic component creation
              samdoyle
              Have a component that is @In @Out set it to the value?
              Have an accessor method which retrieves the component you need when needed? e.g. #{componentCreator.getComponent(whatever)}
              • 4. Re: Programmatic component creation
                dhinojosa

                I was going to go along with Samuel's answer except that I am not a fan of @Out, but to each their own. 


                My solution, if interested, would be:



                @Name("componentCreator")
                public void Parent {
                
                    private Map<String, Object> children;
                
                    @In("componentChildMap")
                    public void setChildren(Map<String, Object> children) {
                         this.children = children;
                    }
                
                    public void makeBabies() {
                        this.children.add(....)
                        this.children.remove(...)
                    }
                }
                



                where you can get the components you need by:



                #{componentChildMap.get['blah'].someProperty}