Now we will see a program to understand the concept of setter injection with map.
1. Customer.java
2. spConfig.xml
3. Test.java
package java8s; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Customer { private Map data; public void setData(Map data) { this.data = data; } public void print() { Set s = data.entrySet(); Iterator itr = s.iterator(); while (itr.hasNext()) { Map.Entry m = (Map.Entry) itr.next(); System.out.println(m.getKey() + " --- " + m.getValue()); } } }
<<? 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:p = "http://www.springframework.org/schema/p" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <beans> <bean id = "id3" class = "java8s.Customer"> <property name = "data"> <map> <entry key = "k101"> <value> 350 </value></entry> <entry key = "k102"> <value> java8s </value> </entry> <entry key = "k103"> <value> 123.456 </value> </entry> </map> </property> </bean> </beans>
Package java8s; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class Test { public static void main(String[] args) { Resource resource = new ClassPathResource("springConfig.xml"); BeanFactory bf = new XmlBeanFactory(resource); Customer c = (Customer) bf.getBean("id3"); c.print(); } }
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc