spring之set注入


实体类

package pojo;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

//@SuppressWarnings("all")
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List hobbys;
    private Map card;
    private Set games;
    private String wife;
    private Properties info;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List getHobbys() {
        return hobbys;
    }

    public void setHobbys(List hobbys) {
        this.hobbys = hobbys;
    }

    public Map getCard() {
        return card;
    }

    public void setCard(Map card) {
        this.card = card;
    }

    public Set getGames() {
        return games;
    }

    public void setGames(Set games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }
    public void show(){
        System.out.println("name="+ name
                + ",address="+ address.getAddress()
                + ",books="
        );
        for (String book:books){
            System.out.print("<<"+book+">>\t");
        }
        System.out.println("\n爱好:"+hobbys);

        System.out.println("card:"+card);

        System.out.println("games:"+games);

        System.out.println("wife:"+wife);

        System.out.println("info:"+info);

    }
}
package pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
"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.xsd">

    "address" class="pojo.Address">
        "address" value="河北省保定市唐县"/>
    

    "student" class="pojo.Student">
        "name" value="窦一萌"/>
        "address" ref="address"/>
        "books">
            
                三国演义
                水浒传
                红楼梦
            
        
        "card">
            
                "身份证号" value="11111111111"/>
                "银行卡号" value="11111111111"/>
            
        
        "games">
            <set>
                王者荣耀
                实况足球
            set>
        
        "hobbys">
            
                踢足球
                羽毛球
                跑步
            
        
        "wife">
            <null/>
        
        "info">
            
                "学号">20193111
                "工号">123456
            
        

    

测试类

    @Test
    public void DiTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml");
        Student student = (Student) context.getBean("student");
        student.show();
    }

运行结果: