In this tutorial you will learn about Spring Map Factory and also see how to use map in spring framework.
In this tutorial you will learn about Spring Map Factory and also see how to use map in spring framework.Ths MapFactoryBean is a simple factory for shared Map instance. The map element is defined in the xml bean definitions. The setSourceMap method set the source Map which is written in xml map element. The SetTargetListClass method set the class to use target Map.
Week.java
package list.factory.example; import java.util.Map; public class Week { private Map days; public Map getDays() { return days; } public void setDays(Map days) { this.days = days; } @Override public String toString() { return "Days [lists=" + days + "]"; } }
AppMain.java
package list.factory.example; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppMain { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "context.xml" }); Week weekday = (Week) context.getBean("week"); System.out.println(weekday); } }
context.xml
<?xml version="1.0" encoding="UTF-8"?>
|
Days [lists={Key2=Monday, Key1=Sunday, Key4=Wednesday, Key3=Tuesday, Key6=Friday, Key5=Thursday, Key7=Saturday}] |