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

首先写个 javabean类吧,如下

[java] view plaincopy
package com.bean; 
 
import java.util.List; 
import java.util.Map; 
import java.util.Properties; 
import java.util.Set; 
 
public class MessageBean { 
    private String username; 
    private String password; 
    private int size; 
    private List<String> citys; 
    private Set<String> friends; 
    private Map<Integer,String> books; 
    private Properties props; 
     
    public void setProps(Properties props) { 
        this.props = props; 
    } 
 
    public void setFriends(Set<String> friends) { 
        this.friends = friends; 
    } 
 
    public void setSize(int size) { 
        this.size = size; 
    } 
 
    public void setPassword(String password) { 
        this.password = password; 
    } 
 
    public void setUsername(String username) { 
        this.username = username; 
    } 
 
    public void show(){ 
        System.out.println(username); 
        System.out.println(password); 
        System.out.println(size); 
        System.out.println("----------"); 
        for(String str:citys){ 
            System.out.println(str); 
        } 
        System.out.println("----------"); 
        for(String str:friends){ 
            System.out.println(str); 
        } 
        System.out.println("---------"); 
        Set<Integer> keys = books.keySet(); 
        for(Integer key:keys){ 
            System.out.println(key+" "+books.get(key)); 
        } 
        System.out.println("---------"); 
        Set params = props.keySet(); 
        for(Object obj:params ){ 
            System.out.println(obj+" : " 
                    +props.getProperty(obj.toString())); 
        } 
    } 
 
    public void setCitys(List<String> citys) { 
        this.citys = citys; 
    } 
 
    public void setBooks(Map<Integer, String> books) { 
        this.books = books; 
    } 


配置文件中配置

[html] view plaincopy
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 
 
<beans> 
    <bean id="userdao" 
         class="com.dao.impl.HibernateUserDAO"> 
    </bean> 
    <!-- setter方式注入 --> 
    <bean id="useraction" 
         class="com.action.UserAction"> 
         <property name="userDao" ref="userdao"> 
         </property> 
    </bean> 
    <!-- 构造器方式注入 --> 
    <bean id="useraction1" 
             class="com.action.UserAction1"> 
             <constructor-arg index="0" ref="userdao"/> 
    </bean> 
    <!-- 各种类型值注入的写法 --> 
    <bean id="messagebean"  
            class="com.bean.MessageBean"> 
            <property name="props"> 
                <props> 
                    <prop key="url">http://www.tom.com</prop> 
                    <prop key="username">zhangsan</prop> 
                    <prop key="password">123456789</prop> 
                </props> 
            </property> 
             
            <property name="books"> 
                <map> 
                    <entry key="10" value="CoreJava"> 
                    </entry> 
                    <entry key="11" value="JavaWeb"> 
                    </entry> 
                    <entry key="12" value="SSH2"> 
                    </entry> 
                </map> 
            </property> 
            <property name="friends"> 
                <set> 
                    <value>张三</value> 
                    <value>李四</value> 
                    <value>王五</value> 
                </set> 
            </property> 
             
            <property name="citys"> 
                <list> 
                    <value>北京</value> 
                    <value>上海</value> 
                    <value>深圳</value> 
                </list> 
            </property> 
             
            <property name="username"> 
                <value>root</value> 
            </property> 
            <property name="password"> 
                <value>1234</value> 
            </property> 
            <property name="size"> 
                <value>15</value> 
            </property> 
    </bean> 
     
</beans> 

代码中调用

[java] view plaincopy
String[] configs = {"applicationContext.xml"}; 
    ApplicationContext ac = 
        new ClassPathXmlApplicationContext(configs); 
    MessageBean msgBean =  
        (MessageBean)ac.getBean("messagebean"); 
    msgBean.show(); 

猜你喜欢

转载自88543006.iteye.com/blog/2241096