1. 程式人生 > 其它 >DI 多種資料型別注入

DI 多種資料型別注入

  首先建立一個Address類

package com.zhang.pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return
"Address{" + "address='" + address + '\'' + '}'; } }
Address類

  然後建立一個Student類,Student類中建立一個Address類物件

package com.zhang.pojo;

import java.util.*;

public class Student {
    private Address address;    //地址
    private String name;        //姓名
    private String[] book;      //
書籍 private List<String> hobby; //愛好 private Map<String, String> card;//學生證 private Set<String> game; //遊戲 private Properties info; //資訊 private String marry; //結婚 public Address getAddress() { return address; } public void setAddress(Address address) {
this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String[] getBook() { return book; } public void setBook(String[] book) { this.book = book; } public List<String> getHobby() { return hobby; } public void setHobby(List<String> hobby) { this.hobby = hobby; } public Map<String, String> getCard() { return card; } public void setCard(Map<String, String> card) { this.card = card; } public Set<String> getGame() { return game; } public void setGame(Set<String> game) { this.game = game; } public Properties getInfo() { return info; } public void setInfo(Properties info) { this.info = info; } public String getMarry() { return marry; } public void setMarry(String marry) { this.marry = marry; } @Override public String toString() { return "Student{" + "address=" + address + ", name='" + name + '\'' + ", book=" + Arrays.toString(book) + ", hobby=" + hobby + ", card=" + card + ", game=" + game + ", info=" + info + ", marry='" + marry + '\'' + '}'; } }
Student類

  配置applicationContext.xml檔案

        注意:map型別和properties型別的資料配置方法特殊

            map:<entry key= " " values= " " >

            propeties:<prop key= " " >  </prop>  在兩個標籤中間填寫資料

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="Address" class="com.zhang.pojo.Address">
        <property name="address" value="北京"/>
    </bean>
    
    <bean id="Student" class="com.zhang.pojo.Student">
        <property name="name" value="小明"/>
        
        <property name="address" ref="Address"/>
        
        <property name="book">
            <array>
                <value>三國演義</value>
                <value>水滸傳</value>
                <value>西遊記</value>
                <value>紅樓夢</value>
            </array>
        </property>
        
        <property name="hobby">
            <list>
                <value>遊戲</value>
                <value>電影</value>
                <value>音樂</value>
            </list>
        </property>
        
        <property name="card">
            <map>
                <entry key="1001" value="小明"/>
                <entry key="1002" value="小剛"/>
                <entry key="1003" value="小李"/>
            </map>
        </property>
        
        <property name="game">
            <set>
                <value>CS</value>
                <value>RDR</value>
                <value>Fortnite</value>
            </set>
        </property>
        
        <property name="info">
            <props>
                <prop key="1001">河北大學</prop>
                <prop key="1002">河南大學</prop>
                <prop key="1003">河海大學</prop>
            </props>

        </property>
        <property name="marry">
            <null/>
        </property>
    </bean>

</beans>

  MyTest類測試結果

import com.zhang.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    @Test
    public void StudentTest() {
        Student student = (Student) context.getBean("Student");
        System.out.println(student.toString());

    }
}

  結果: