Sunday 20 March 2011

Spring Map Example

In this example you will see how bean is prepared for injecting Map collection type key and its values.

import java.util.Iterator;
import java.util.Map;

public class MapBean {
private Map<String, Integer> student;
public void setDetails(Map<String, Integer> student) {
this.student = student;
}
public void showDetails() {
Iterator it = student.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
}
}


In the config file :


<bean id="mapbean" class="com.xxxx.MapBean">
<property name="details">
<map>
<entry key="Satya" value="101"/>
<entry key="Rohit" value="102"/>
<entry key="Aniket" value="103"/>
</map>
</property>
</bean>

No comments:

Post a Comment