spring-注入map集合
一.創(chuàng)建項目
?? ?項目名稱:spring092901
二.添加jar包
?? ?commons-logging.jar
?? ?junit-4.4.jar
?? ?log4j.jar
?? ?spring-beans-3.2.0.RELEASE.jar
?? ?spring-context-3.2.0.RELEASE.jar
?? ?spring-core-3.2.0.RELEASE.jar
?? ?spring-expression-3.2.0.RELEASE.jar
三.添加配置文件
?? ?1.在項目中創(chuàng)建conf目錄
?? ??? ?/conf
?? ?2.在conf目錄下添加配置文件
?? ??? ?配置文件名稱:applicationContext.xml
?? ??? ?配置文件內(nèi)容:
?? ??? ?<?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"
?? ??? ??????? xmlns:util="http://www.springframework.org/schema/util"
?? ??? ??????? xsi:schemaLocation="
?? ??? ?http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
?? ??? ?http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
?? ??? ??? ?
?? ??? ?</beans>
?? ??? ?
四.創(chuàng)建業(yè)務bean
?? ?1.在src下創(chuàng)建包
?? ??? ?包名:cn.jbit.spring092901.collection
?? ?2.在包下創(chuàng)建bean
?? ??? ?bean名稱:MapBean.java
?? ??? ?bean內(nèi)容:
?? ??? ?public class MapBean {
?? ??? ??? ?private Map map;
?? ??? ?
?? ??? ??? ?public Map getMap() {
?? ??? ??? ??? ?return map;
?? ??? ??? ?}
?? ??? ?
?? ??? ??? ?public void setMap(Map map) {
?? ??? ??? ??? ?this.map = map;
?? ??? ??? ?}
?? ??? ?}
?? ?3.在核心配置文件中配置bean
?? ??? ?<bean id="mapbean" class="cn.jbit.spring092901.collection.MapBean">
?? ??? ??? ?<property name="map">
?? ??? ??? ??? ?<map>
?? ??? ??? ??? ??? ?<entry key="name1" value="詩"></entry>
?? ??? ??? ??? ??? ?<entry key="date" value="春秋時期"></entry>
?? ??? ??? ??? ?</map>
?? ??? ??? ?</property>
?? ??? ?</bean>
五.測試
?? ?1.在項目中創(chuàng)建test目錄
?? ??? ?/test
?? ?2.在test目錄下創(chuàng)建包
?? ??? ?cn.jbit.spring092901.collection
?? ?3.在包下 創(chuàng)建測試類
?? ??? ?類名:MapBeanTest.java
?? ??? ?類內(nèi)容:
?? ??? ?public class MapBeanTest {
?? ??? ??? ?@Test
?? ??? ??? ?public void testListBean(){
?? ??? ??? ??? ?ClassPathXmlApplicationContext cpac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
?? ??? ??? ??? ?MapBean mapBean = (MapBean) cpac.getBean("mapbean");
?? ??? ??? ??? ?Map map = mapBean.getMap();
?? ??? ??? ??? ?Set set = map.keySet();
?? ??? ??? ??? ?Iterator it = set.iterator();
?? ??? ??? ??? ?while(it.hasNext()){
?? ??? ??? ??? ??? ?String key = (String) it.next();
?? ??? ??? ??? ??? ?String value = (String) map.get(key);
?? ??? ??? ??? ??? ?System.out.println(value);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ?}
轉(zhuǎn)載于:https://blog.51cto.com/suyanzhu/1559510
總結(jié)
以上是生活随笔為你收集整理的spring-注入map集合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 搞懂function(*args,**k
- 下一篇: 微软发布Win 10 开始按钮真正回归明