「Spring開発」xmlで
1.Book.java
import java.util.Map;
public class Book {
private String title;
private Map<String, String> pubInfo;
public Map<String, String> getpubInfo() {
return pubInfo;
}
public void setpubInfo(Map<String, String> pubInfo) {
this.pubInfo = pubInfo;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
2.spring-beans.xml
<?xml version="1.0″ encoding="UTF-8″?>
<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="book" class="Book">
<property name="title" value="Spring MVC開発"/>
<property name="pubInfo">
<map>
<entry key="1″ value="山田" />
<entry key="2″ value="鈴木"/>
<entry key="3″ value="北村" />
</map>
</property>
</bean>
</beans>
3.mainメソッド
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BookPubInfo {
@SuppressWarnings(“resource")
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {“spring-beans.xml"});
Book book= (Book)appContext.getBean(“book");
System.out.println(“本の名前:");
System.out.println(book.getTitle());
Map<String, String> pubInfo = book.getpubInfo();
System.out.println(“筆者: “);
for(String key : pubInfo.keySet() ){
System.out.println(key + " : “+(String)pubInfo.get(key));
}
}
}
実行結果:
本の名前:
Spring MVC開発
筆者:
1 : 山田
2 : 鈴木
3 : 北村