Spring学习笔记:03 DI 依赖注入

1,构造器注入:

个人理解是使用构造方法来创建对象的方法称为构造器注入

constructor-arg(有参)和property(无参)

2,set注入(重点):

  • 依赖注入:set注入
    - 依赖:bean对象的创建依赖于容器
    - 注入:bean对象中的所有属性,由容器来注入
package com.spring.people;

import java.util.*;

public class Student {
    private String name;
    private Adress adress;//这是一个自定义的类
    private String[] books;
    private List<String> hobby;
    private Map<String,String> card;
    private Set<String> game;
    private Properties info;
    private String wife;

    public Student() {
    }
    public String getName() {
        return name;
    }

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

    public Adress getAdress() {
        return adress;
    }

    public void setAdress(Adress adress) {
        this.adress = adress;
    }

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

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

    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 getWife() {
        return wife;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", adress=" + adress +
                ", books=" + Arrays.toString(books) +
                ", hobby=" + hobby +
                ", card=" + card +
                ", game=" + game +
                ", info=" + info +
                ", wife='" + wife + '\'' +
                '}';
    }
}
    <?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.spring.people.Adress">
        <property name="address" value="杜王町"/>
    </bean>
    <bean id="student" class="com.spring.people.Student">
        <!--普通注入-->
        <property name="name" value="zhangsan"></property>
        <!--bean注入-->
        <property name="adress" ref="address"/>
        <!--数组注入-->
        <property name="books" >
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>三国演义</value>
                <value>水浒传</value>
            </array>
        </property>
        <!--list注入-->
        <property name="hobby">
            <list>
                <value>java</value>
                <value>c</value>
                <value>python</value>
            </list>
        </property>
        <!--map注入-->
        <property name="card">
            <map>
                <entry key="a" value="a1"/>
                <entry key="b" value="b1"/>
                <entry key="c" value="c1"/>
            </map>
        </property>
        <!--Set注入-->
        <property name="game">
            <set>
                <value>lol</value>
                <value>ow</value>
            </set>
        </property>
        <!--null-->
        <property name="wife">
            <null></null>
        </property>
        <!--properties-->
        <property name="info">
            <props>
                <prop key="学号">2010</prop>
                <prop key="性别">男</prop>
            </props>
        </property>
    </bean>
</beans>


    


上一篇:阿里云支付:可以更换绑定的支付宝账号吗?


下一篇:HashMap和Hashtable的详细区别