4、依赖注入(DI)

4.1 构造器注入

前面已经过了

4.2 Set方式注入【重点】

依赖注入:本质是set方法注入

  • 依赖:bean对象的创建依赖于容器
  • 注入:bean对象中的所有属性,由容器来注入

环境搭建

1、复杂类型

1、引用对象

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

2、实际测试对象

public class Student {

    private String name;
    private Address address;
    private String [] books;
    private List<String> hobbies;
    private Map<String, String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    // 以及getter、setter、toString方法
    
}

3、applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.wen.pojo.Address">
        <property name="address" value="天堂"/>
    </bean>

    <bean name="student" class="com.wen.pojo.Student">
        <!--普通值注入,value-->
        <property name="name" value="神仙"/>

        <!--引用类型注入,ref-->
        <property name="address" ref="address"/>

        <!--数组注入-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>三国演义</value>
            </array>
        </property>

        <!--List-->
        <property name="hobbies">
            <list>
                <value>敲代码</value>
                <value>看电影</value>
                <value>读书</value>
            </list>
        </property>

        <!--Map-->
        <property name="card">
            <map>
                <entry key="身份证" value="232121312"/>
                <entry key="银行卡" value="293823801"/>
            </map>
        </property>

        <!--Set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>CS</value>
            </set>
        </property>

        <!--null值注入-->
        <property name="wife">
            <null/>
        </property>

        <!--Properties
        key = value-->
        <property name="info">
            <props>
                <prop key="学号">20210327</prop>
                <prop key="性别">男</prop>
            </props>
        </property>
    </bean>

</beans>

4、测试类

    @Test
    public void test(){

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        System.out.println(context.getBean("student", "Student.class"));

    }
/*
Student{
name='神仙',
address=Address{address='天堂'}, 
books=[红楼梦, 西游记, 水浒传, 三国演义],
hobbies=[敲代码, 看电影, 读书],
card={身份证=232121312, 银行卡=293823801}, 
games=[LOL, CS], wife='null', 
info={学号=20210327, 性别=男}
}
*/

4.3 p、c标签注入

p命名和c命名空间需要导入约束

Shortcut with the p-namespace

xmlns:p="http://www.springframework.org/schema/p"

spring(四)依赖注入及bean的作用域_xml

spring(四)依赖注入及bean的作用域_依赖注入_02

Shortcut with the c-namespace

必须要有有参和无参构造器

xmlns:c="http://www.springframework.org/schema/c"

spring(四)依赖注入及bean的作用域_xml_03

4.4 Bean作用域

spring(四)依赖注入及bean的作用域_web开发_04

1、单例模式(默认机制)

spring(四)依赖注入及bean的作用域_xml_05

<bean id="accountService" class="com.something.DefaultAccountService"/>

<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>

2.原型模式:每次从容器中get对象时,都重新创建

spring(四)依赖注入及bean的作用域_web开发_06

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