spring <Map>元素用來存儲多個鍵值對屬性,類型爲Java.util.Map;他的子標籤<entry>用來定義Map中的鍵值實體,下面舉例說明;java
Article.javaspring
這個article class有一個屬性是做者聯名信息,使用序號和做者名來構成一個Map屬性.app
import java.util.*;測試
public class Articlethis
{ .net
private String title;xml
private Map<String, String> authorsInfo;get
public void setTitle(String title) {it
this.title = title;io
}
public String getTitle() {
return title;
}
public void setAuthorsInfo(Map<String, String> authorsInfo) {
this.authorsInfo = authorsInfo;
}
public Map<String, String> getAuthorsInfo() {
return authorsInfo;
}
}
spring-beans.xml:
<map>元素用於提供具體的實體鍵值配置,經過<entry>將序號和做者名稱進行綁定注入。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="article" class="Article">
<property name="title" value="RoseIndia"/>
<property name="authorsInfo">
<map>
<entry key="1" value="Deepak" />
<entry key="2" value="Arun"/>
<entry key="3" value="Vijay" />
</map>
</property>
</bean>
</beans>
RunDemoMain.java
測試主程代碼;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.*;
public class AppMain
{
public static void main( String[] args )
{
ApplicationContext appContext =
new ClassPathXmlApplicationContext(new String[] {"spring-beans.xml"});
Article article = (Article)appContext.getBean("article");
System.out.println("Article Title: "+article.getTitle());
Map<String, String> authorsInfo = article.getAuthorsInfo();
System.out.println("Article Author: ");
for (String key : authorsInfo.keySet()) {
System.out.println(key + " : "+(String)authorsInfo.get(key));
}
}
}
輸出結果以下:
Article Title: RoseIndia
Article Author:
1 : Deepak
2 : Arun
3 : Vijay