Results 1 to 8 of 8

Thread: how to define an ArrayList of objects in xml

  1. #1

    Default 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. #2

    Default

    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...

  3. #3
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    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>

  4. #4

    Default

    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...

  5. #5
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

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

  6. #6

    Default

    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."

  7. #7
    Join Date
    Aug 2005
    Posts
    28

    Default

    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

  8. #8
    Join Date
    Aug 2004
    Location
    Atlanta, GA
    Posts
    129

    Default

    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
    Randy

Similar Threads

  1. Replies: 1
    Last Post: Jun 24th, 2007, 10:58 AM
  2. Replies: 20
    Last Post: Aug 31st, 2006, 08:34 PM
  3. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  4. Replies: 2
    Last Post: Jun 16th, 2005, 04:15 PM
  5. Should Business Objects have References to DAOs?
    By sethladd in forum Architecture
    Replies: 9
    Last Post: Aug 25th, 2004, 01:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •