1. 程式人生 > 其它 >Spring_DI注入依賴

Spring_DI注入依賴

依賴注入:set注入

依賴:bean物件的建立依賴於容器。
注入:bean物件中的所有屬性,由容器來注入。

實體類:

點選檢視程式碼
package com.cn.demo;

import java.util.*;

public class Student {
    private String name;//value
    private Address address;//ref
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private Properties info;
    private String wife;

    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<String> getHobbys() {
        return hobbys;
    }

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

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

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

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    public String getWife() {
        return wife;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", info=" + info +
                ", wife='" + wife + '\'' +
                '}';
    }
}


點選檢視程式碼
package com.cn.demo;

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 + '\'' +
                '}';
    }
}


xml檔案

點選檢視程式碼
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">



    <bean id="address" class="com.cn.demo.Address">
        <property name="address" value="武漢市"/>
    </bean>

    <bean id="student2" class="com.cn.demo.Student">
        <!--普通值注入-->
        <property name="name" value="楊晨"/>

        <!--bean注入-->
        <property name="address" ref="address"/>

        <!--陣列注入-->
        <property name="books">
            <array>
                <value>平凡的世界</value>
                <value>追著風箏的人</value>
                <value>活著</value>
            </array>
        </property>

        <!--list注入-->
        <property name="hobbys">
            <list>
                <value>打籃球</value>
                <value>玩遊戲</value>
                <value>看電影</value>
            </list>
        </property>

        <!--Map注入-->
        <property name="card">
            <map>
                <entry key="學號" value="020321752326"/>
                <entry key="學院" value="計算機學院"/>
                <entry key="班級" value="523"/>
            </map>
        </property>

        <!--set注入-->
        <property name="games">
            <set>
                <value>英雄聯盟</value>
                <value>王者榮耀</value>
            </set>
        </property>

        <!--null注入-->
        <property name="wife">
            <null/>
        </property>
        <!--Properties注入-->
        <property name="info">
            <props>
                <prop key="學號">020321752326</prop>
                <prop key="姓名">楊晨</prop>
            </props>
        </property>
    </bean>




</beans>

測試:

點選檢視程式碼
import com.cn.demo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentTest{
    public static void main(String[] args) {
        /*獲取Spring的上下文物件*/
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        /*我們的物件交給Spring去管理了,我們要使用,直接去取出來即可*/
        Student student = (Student) context.getBean("student2");
        System.out.println(student.toString());


    }

        }

結果截圖: