Spring入门之bean属性注入集合

Spring中的bean的属性值有时候包含一些集合类型的属性值,注入属性值的方式和注入一些基本的属性值不一样,他们需要使用专门的方式来注入
常用的集合类型包括: Map、Set、List、数组、属性集合
实例代码:
//person类
public class Person {

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }

}


//测试bean类
public class AllSetBean {

    private String[] names;
    private List personlist;
    private Set personset;
    private Map personmap;
    private Properties personprop;

    public String[] getNames() {
        return names;
    }
    public void setNames(String[] names) {
        this.names = names;
    }
    public List getPersonlist() {
        return personlist;
    }
    public void setPersonlist(List personlist) {
        this.personlist = personlist;
    }
    public Set getPersonset() {
        return personset;
    }
    public void setPersonset(Set personset) {
        this.personset = personset;
    }
    public Map getPersonmap() {
        return personmap;
    }
    public void setPersonmap(Map personmap) {
        this.personmap = personmap;
    }
    public Properties getPersonprop() {
        return personprop;
    }
    public void setPersonprop(Properties personprop) {
        this.personprop = personprop;
    }

}


//beans.xml配置文件
"person1" class="org.spring.IocSet.Person">
            "name" value="成兮" />
            "age" value="20" />
        
        "person2" class="org.spring.IocSet.Person">
            "name" value="缘分五月" />
            "age" value="20" />
        
        "allSet" class="org.spring.IocSet.AllSetBean">
            
            "names">
                
                    <value>成兮value>
                    <value>缘分五月value>
                
            

            
            "personlist">
                
                    <ref bean="person1" />
                    <ref bean="person2" />
                
            

            
            "personset">
                <set>
                    <ref bean="person1" />
                    <ref bean="person2" />
                set>
            

            
            "personmap">
                
                    "1" value-ref="person1" />
                    "2" value-ref="person2" />
                
            

            
            "personprop">
                
                    "username">chengxi
                    "password">970624
                
            
        


//测试主程序
public class TestMain {

    public static void main(String[] args){

        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        AllSetBean bean = context.getBean("allSet",AllSetBean.class);

        //测试数组
        String[] names = bean.getNames();
        System.out.println("测试数组");
        for(String name: names){
            System.out.println("name->"+name);
        }

        //测试List
        List personl = bean.getPersonlist();
        System.out.println("测试List");
        for(Person person: personl){
            System.out.println("person.name->"+person.getName()+"\tperson.age->"+person.getAge());
        }

        //测试Set
        Set persons = bean.getPersonset();
        System.out.println("测试Set");
        for(Person person: persons){
            System.out.println("person.name->"+person.getName()+"\tperson.age->"+person.getAge());
        }

        //测试Map
        Map personm = bean.getPersonmap();
        System.out.println("测试Map");
        Iterator it = personm.keySet().iterator();
        while(it.hasNext()){
            Integer key = (Integer) it.next();
            Person person = personm.get(key);
            System.out.println("key->"+key+"\tperson.name->"+person.getName()+"\tperson.age->"+person.getAge());
        }

        //Property属性值测试
        Properties personprop = bean.getPersonprop();
        System.out.println("测试Properties属性");
        for(Entry entry:personprop.entrySet()){  
            System.out.println(entry.getKey().toString()+" "+entry.getValue().toString());  
        }  
        System.out.println("*****通过Enumeration取出*****");  
        Enumeration en= personprop.keys();  
        while(en.hasMoreElements()){  
            String key=(String) en.nextElement();  
            System.out.println(key+" "+personprop.getProperty(key));  
        }  
    }
}
文档转载自: Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件

你可能感兴趣的:(java-web开发,Spring,ssm)