0

Is there a way to iterate list or map in spring? I am not able to find any references for this online.

This is what I defined-

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">


<util:list id="myList" value-type="java.lang.String">
    <value>foo</value>
    <value>bar</value>
</util:list>

<bean id="bean1" class="com.StaticClass" factory-method="createObject">
    <constructor-arg value="foo" />
</bean>
<bean id="bean2" class="com.StaticClass" factory-method="createObject">
    <constructor-arg value="bar" />
</bean>

<bean id="myMap" class="java.util.HashMap">
    <constructor-arg index="0" type="java.util.Map">
        <map key-type="java.lang.Integer" value-type="java.lang.Float">
            <entry key="foo" value-ref=bean1 />
            <entry key="bar" value-ref=bean2 />
        </map>
    </constructor-arg>
</bean>

Instead of creating multiple bean objects, I want to iterate over this list and create a map, using following logic-

for (String m : myList) {
    myMap.put(m, MyStaticFactory.createObject(m));
}

Can I do this in Spring?

Teja
  • 341
  • 2
  • 7
  • 18
  • 2
    What does this have to do with Spring? – Chris Martin Jan 08 '15 at 23:50
  • I am trying to create this map in spring. I am stuck with two things here- 1) iterating list in spring and 2) calling a static method to get the value and insert it in map. – Teja Jan 08 '15 at 23:52
  • 1
    It is pretty unclear what this has to do with Spring, and actually what you are trying to accomplish. – mkobit Jan 08 '15 at 23:56
  • Updated the question explaining the exact problem what I am trying to solve. Hope it is clear now. – Teja Jan 09 '15 at 00:06

2 Answers2

1

How about using spring @Configuration (see explanation in this link) instead of spring XML?

@Configuration
public class MySpringContext {

   @Bean(name="myMap")
   public Map<String, StaticClass> getMyMapBean() {
      // I'm not sure where you create 'm' but if that's a bean you can inject it to the class and use it.
      for (String m : myList) {
        myMap.put(m, MyStaticFactory.createObject(m));
      }
   }
}

@Configuration classes are a way to define your beans programatically instead of XMLs which gives you much more flexibility to do whatever you want.

Community
  • 1
  • 1
Avi
  • 21,182
  • 26
  • 82
  • 121
0

Something like this maybe:

public class MyMapBean extends HashMap {
   public MyMapBean(List<String> beanNames) {
       for(name: beanNames) put(name, MyStaticFactory.createObject(name)); 
   }
}

and then in application context:

<bean id="myMap" class="MyMapBean">
    <constructor-arg index="0" value-ref="myList" />       
</bean>
Dima
  • 39,570
  • 6
  • 44
  • 70
  • Yes. I could do that. So, there is no way to do it completely in spring itself? – Teja Jan 09 '15 at 00:40
  • I don't know what you mean by "spring itself". The original xml in your question is kinda "doing it completely in spring itself". I thought, you did not want to do it. – Dima Jan 09 '15 at 00:55