Announcement Announcement Module
Collapse
No announcement yet.
how to define an ArrayList of objects in xml Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to define an ArrayList of objects in xml

    I am testing my spring application, now what i want to do is define an arraylist to be used in my application.

    i am trying this way but i am having errors:

    Code:
    <bean id="testList" class="java.util.ArrayList">
    	<list>
    		<value>Test</value>
    		<ref bean="testViewBean"/>
    	</list>
    </bean>
    what i want to do is like this (in java code terminology)

    Code:
    ArrayList testList = new ArrayList&#40;&#41;;
    bean1 = new MyBean&#40;&#41;;
    bean2 = new MyBean&#40;&#41;;
    bean3 = new MyBean&#40;&#41;;
    testList.add&#40;bean1&#41;;
    testList.add&#40;bean2&#41;;
    testList.add&#40;bean3&#41;;
    now testList will be used a parameter for one of my business class.

    i am asking this because in my test i need to submit an ArrayList of MyBean() as parameter for one of my business class.

    Code:
    	public void testSimpleCheck&#40;&#41; &#123;
                    ArrayList testList = &#40;ArrayList&#41;this.applicationContext.getBean&#40;"testList"&#41;;
                    for&#40;int i=0;i<testList.size&#40;&#41;;i++&#41;&#123;
                           MyBean bean = &#40;MyBean&#41; testList.get&#40;i&#41;;
                           ...........
                    &#125;		
    	&#125;
    maybe someone can show me the proper way to do this in xml.

    thanks a lot guys.

  • #2
    or guys can how can i instantiate a class whose constructor is ArrayList

    example:
    Code:
    public class MyObjects&#123;
        public MyObjects&#40;ArrayList lists&#41;&#123;
        &#125;
    &#125;
    
    
    how will i be able this to define in my xml file&#58;
    
    <bean id="myBean1" type="MyBean">
             .....
    <bean>
    
    <bean id="myBean2" type="MyBean">
             .....
    <bean>
    
    <bean id="myArrayList" type="java.util.ArrayList">
        <list>????
             <ref bean="myBean1"/>????
             .....
             <ref bean="myBean2"/>????
        </list>????
    </bean>
    
    <bean id="myObjects" class="MyObjects">
         <constructor-arg type="java.util.ArrayList" ref ="myArrayList"/>
    </bean>
    
    my problem is how to fill-up the myArrayList object which is of type Java.util.ArrayList

    guys, please help me out. thx...

    Comment


    • #3
      You can make the list of objects just be a property of the object..

      Code:
      <bean id="myObjects"  ....... >
      <property name="listOfBeans">
           <list>
               <ref bean="beanOne"/>
               <ref bean="beanTwo"/>
           </list>
      </property>
      </bean>

      Comment


      • #4
        so you would suggest giving my class an empty constructor and put all i need as a setter or property of the class?

        well , i can do that, but is there really no other way to put ArrayList as your constructor-arg or instantiate an ArrayList as one of your beans?

        anyway, guys i think i should just make a setter for now, just to continue my testing and development.

        thanks a lot.

        any more suggestion and/or solutions is welcome.

        thanks again and more power...

        Comment


        • #5
          You can use the same format of xml for constructor-arg too.
          Though I have not done so or recall an example.

          Comment


          • #6
            guys, this works.

            Code:
            <bean id="testList" class="java.util.ArrayList">
            	<constructor-arg>
            	<list>
            		<ref bean="testViewBean"/>
            		<ref bean="testViewBean2"/>
            	</list>
            	</constructor-arg>
            </bean>

            "keep the faith."

            Comment


            • #7
              since this topic is already dealing with it, i might add to the question:

              what if you do not know how many elements the arraylist will have?
              example:

              Code:
              public class MyBean &#123;
                    private ArrayList logs;
              
                     // getter and setter...
              &#125;
              &#125;
              
              beans.xml&#58;
              <bean id="log" class="my.package.Log"/>
              <bean id="myBean" class="my.package.MyBean">
                    <property name="logs">
                           <list><ref bean="log"/></list>
                    </property>
              </bean>
              how will spring react to this? What I want it to do really is on creation of myBean during the initialisation, not to fill the array because the logs are fetched from a database with hibernate.

              I'm a little confused about this

              Comment


              • #8
                One way to load objects from a database would be to use MethodInvokingFactoryBean to call the method on your DAO.

                For example:
                Code:
                <bean id="target" class="a.b.c.TargetBean">
                    <property name="logs">
                        <ref bean="logFactory"/>
                    </property>
                </bean>
                
                <bean id="logFactory" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                    <property name="targetObject"/>
                        <ref bean="logDao"/>
                    </property>
                    <property name="targetMethod" value="getLogs"/>
                </bean>
                where your dao (not defined) returns a list of logs.
                Disclaimer: Code just typed in, not tested :shock:

                HTH

                Comment

                Working...
                X