spring 3配置文件中如何注入map list set等类型

首先写个 javabean类吧,如下

 

[java][/java] view plaincopy

  1. package com.bean;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.Properties;
  5. import java.util.Set;
  6. public class MessageBean {
  7.     private String username;
  8.     private String password;
  9.     private int size;
  10.     private List<String> citys;
  11.     private Set<String> friends;
  12.     private Map<Integer,String> books;
  13.     private Properties props;
  14.     public void setProps(Properties props) {
  15.         this.props = props;
  16.     }
  17.     public void setFriends(Set<String> friends) {
  18.         this.friends = friends;
  19.     }
  20.     public void setSize(int size) {
  21.         this.size = size;
  22.     }
  23.     public void setPassword(String password) {
  24.         this.password = password;
  25.     }
  26.     public void setUsername(String username) {
  27.         this.username = username;
  28.     }
  29.     public void show(){
  30.         System.out.println(username);
  31.         System.out.println(password);
  32.         System.out.println(size);
  33.         System.out.println(“———-“);
  34.         for(String str:citys){
  35.             System.out.println(str);
  36.         }
  37.         System.out.println(“———-“);
  38.         for(String str:friends){
  39.             System.out.println(str);
  40.         }
  41.         System.out.println(“———“);
  42.         Set<Integer> keys = books.keySet();
  43.         for(Integer key:keys){
  44.             System.out.println(key+” “+books.get(key));
  45.         }
  46.         System.out.println(“———“);
  47.         Set params = props.keySet();
  48.         for(Object obj:params ){
  49.             System.out.println(obj+” : “
  50.                     +props.getProperty(obj.toString()));
  51.         }
  52.     }
  53.     public void setCitys(List<String> citys) {
  54.         this.citys = citys;
  55.     }
  56.     public void setBooks(Map<Integer, String> books) {
  57.         this.books = books;
  58.     }
  59. }

配置文件中配置

 

 

 

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN 2.0//EN” “http://www.springframework.org/dtd/spring-beans-2.0.dtd”>
  3. <beans>
  4.     <bean id=”userdao”
  5.          class=”com.dao.impl.HibernateUserDAO”>
  6.     </bean>
  7.     <!– setter方式注入 –>
  8.     <bean id=”useraction”
  9.          class=”com.action.UserAction”>
  10.          <property name=”userDao” ref=”userdao”>
  11.          </property>
  12.     </bean>
  13.     <!– 构造器方式注入 –>
  14.     <bean id=”useraction1″
  15.              class=”com.action.UserAction1″>
  16.              <constructor-arg index=”0″ ref=”userdao”/>
  17.     </bean>
  18.     <!– 各种类型值注入的写法 –>
  19.     <bean id=”messagebean”
  20.             class=”com.bean.MessageBean”>
  21.             <property name=”props”>
  22.                 <props>
  23.                     <prop key=”url”>http://www.tom.com</prop>
  24.                     <prop key=”username”>zhangsan</prop>
  25.                     <prop key=”password”>123456789</prop>
  26.                 </props>
  27.             </property>
  28.             <property name=”books”>
  29.                 <map>
  30.                     <entry key=”10″ value=”CoreJava”>
  31.                     </entry>
  32.                     <entry key=”11″ value=”JavaWeb”>
  33.                     </entry>
  34.                     <entry key=”12″ value=”SSH2″>
  35.                     </entry>
  36.                 </map>
  37.             </property>
  38.             <property name=”friends”>
  39.                 <set>
  40.                     <value>张三</value>
  41.                     <value>李四</value>
  42.                     <value>王五</value>
  43.                 </set>
  44.             </property>
  45.             <property name=”citys”>
  46.                 <list>
  47.                     <value>北京</value>
  48.                     <value>上海</value>
  49.                     <value>深圳</value>
  50.                 </list>
  51.             </property>
  52.             <property name=”username”>
  53.                 <value>root</value>
  54.             </property>
  55.             <property name=”password”>
  56.                 <value>1234</value>
  57.             </property>
  58.             <property name=”size”>
  59.                 <value>15</value>
  60.             </property>
  61.     </bean>
  62. </beans>

代码中调用

 

 

 

[java][/java] view plaincopy

  1. String[] configs = {“applicationContext.xml”};
  2.     ApplicationContext ac =
  3.         new ClassPathXmlApplicationContext(configs);
  4.     MessageBean msgBean =
  5.         (MessageBean)ac.getBean(“messagebean”);
  6.     msgBean.show();

标签