Spring学习-DI依赖注入
构造器注入
见本专栏中 Spring学习-第一个Spring程序&IoC创建对象的方式
Set方式注入【重点】
- 依赖注入:set注入 
  - 依赖:bean对象的创建依赖于容器
- 注入:bean对象的所有属性,由容器来注入
 
【环境搭建】
- 复杂类型
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 + '\'' +
                '}';
    }
}
- 真实测试对象
package com.lyh.pojo;
import java.util.*;
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> 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<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 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;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}
- beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="student" class="com.lyh.pojo.Student">
        <!--第一种:普通值注入,value-->
        <property name="name" value="admin" />
    </bean>
</beans>
- 测试
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.getName());
    }
}
- 完善注册信息
<?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.lyh.pojo.Address" >
        <property name="address" value="山东" />
    </bean>
    <bean id="student" class="com.lyh.pojo.Student">
        <!--第一种:普通值注入,value-->
        <property name="name" value="admin" />
        <!--第二种,Bean注入,ref-->
        <property name="address" ref="address" />
        <!--数组注入,ref-->
        <property name="books">
            <array>
                <value>数据结构</value>
                <value>计算机组成原理</value>
                <value>操作系统</value>
                <value>计算机网络</value>
            </array>
        </property>
        <!--List-->
        <property name="hobbys">
            <list>
                <value>综艺</value>
                <value>代码</value>
                <value>游戏</value>
                <value>电视剧</value>
            </list>
        </property>
        <!--Map-->
        <property name="card">
            <map>
                <entry key="身份证" value="370829200104029999" />
                <entry key="银行卡" value="31611642134431341643" />
            </map>
        </property>
        <!--Set-->
        <property name="games">
            <set>
                <value>王者荣耀</value>
                <value>香肠派对</value>
                <value>百变大侦探</value>
            </set>
        </property>
        <!--null-->
        <property name="wife">
            <null />
        </property>
        <!--Properties-->
        <property name="info">
            <props>
                <prop key="学号">2018XXXXXX</prop>
                <prop key="性别">女</prop>
                <prop key="姓名">lyh</prop>
            </props>
        </property>
    </bean>
</beans>
拓展方式注入
可以使用p命名空间和c命名空间注入
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--p命名空间注入,set注入,可以直接注入属性的值:property-->
    <bean id="user" class="com.lyh.pojo.User" p:name="admin" p:age="18" />
    <!--c命名空间注入,通过构造器注入:construct-args-->
    <bean id="user" class="com.lyh.pojo.User" c:age="18" c:name="admin"/>
    
</beans>
注意点:P命名和c命名空间不能直接使用,需要在需要使用的spring配置文件中导入xml配置
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:c="http://www.springframework.org/schema/c"
bean的作用域

| Scope | 描述 | 
|---|---|
| singleton | (默认)将单个 bean 定义限定为每个 Spring IoC 容器的单个对象实例。 | 
| prototype | 将单个 bean 定义限定为任意数量的对象实例。 | 
| request | 将单个 bean 定义限定为单个 HTTP 请求的生命周期。也就是说,每个 HTTP 请求都有自己的 bean 实例,该实例是在单个 bean 定义的后面创建的。仅在 Web 感知 Spring 的上下文中有效ApplicationContext。 | 
| session | 将单个 bean 定义限定为 HTTP 的生命周期Session。仅在 Web 感知 Spring 的上下文中有效ApplicationContext。 | 
| application | 将单个 bean 定义限定为ServletContext. 仅在 Web 感知 Spring 的上下文中有效ApplicationContext。 | 
| websocket | 将单个 bean 定义限定为WebSocket. 仅在 Web 感知 Spring 的上下文中有效ApplicationContext。 | 
- 单例模式(Spring默认机制)
<bean id="user" class="com.lyh.pojo.User" c:age="18" c:name="admin" scope="singleton"/>

- 原型模式:每次从容器中get的时候,都会产生一个新对象
<bean id="user" class="com.lyh.pojo.User" c:age="18" c:name="admin" scope="prototype"/>

 3. 其余的request、session、application这些只能在web开发中使用到!










