依赖简介

一个典型的企业应用程序不是由一个单一的对象组成(或Spring的说法中的bean)。即使是最简单的应用程序也只有几个对象一起工作来呈现最终用户看作是一个连贯的应用程序。如何从定义许多独立的bean定义到完全实现的应用程序,在这些应用程序中对象协作实现目标。

依赖注入

依赖注入(DI)是一个过程,通过这个过程,对象可以通过构造函数参数,工厂方法的参数或者在构造或返回对象实例后设置的属性来定义它们的依赖关系从工厂方法。然后容器在创建bean时注入这些依赖关系。这个过程从根本上说是相反的,因此名为控制反转(IoC),它本身通过使用类的直接构造或服务定位符模式来控制它自己的依赖关系的实例化或位置。
代码与DI原则相比更加清晰,当对象提供依赖时,解耦更为有效。该对象不查找它的依赖关系,不知道依赖关系的位置或类。因此,您的类变得更容易测试,特别是当依赖关系在接口或抽象基类上时,它们允许在单元测试中使用存根或模拟实现。

DI存在两种主要的变体,基于构造函数的依赖注入和基于Setter的依赖注入

Spring DI依赖注入详解

pojo类:

public class Student {
    private String name;
    private Hello hello;
    private String[] books;
    private List<String> hobbys;
    private Map<String, String> games;
    private String wife;
    private Properties info;
    
	public Student() {
	}

    public Student(String name, String wife) {
        this.name = name;
        this.wife = wife;
    }

    public String getName() {
        return name;
    }

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

    public Hello getHello() {
        return hello;
    }

    public void setHello(Hello hello) {
        this.hello = hello;
    }

    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> getGames() {
        return games;
    }

    public void setGames(Map<String, 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 + '\'' +
                ", hello=" + hello +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

注入普通的String属性:

<bean id="student" class="top.imustctf.pojo.Student">
    <property name="name" value="dahe"/>
</bean>

bean注入,适用于其他的实体类:

<bean id="student" class="top.imustctf.pojo.Student">
    <property name="hello" ref="hello"/>
</bean>

数组注入:

<bean id="student" class="top.imustctf.pojo.Student">
    <property name="books">
        <array>
            <value>C语言入门到精通</value>
            <value>Spring底层原理</value>
        </array>
    </property>
</bean>

List注入:

<bean id="student" class="top.imustctf.pojo.Student">
    <property name="hobbys">
        <list>
            <value>编程</value>
            <value>美女</value>
        </list>
    </property>
</bean>

Map注入:

<bean id="student" class="top.imustctf.pojo.Student">
    <property name="games">
        <map>
            <entry key="王者荣耀" value="30级"/>
            <entry key="我的世界" value="100级"/>
        </map>
    </property>
</bean>

空值注入:

<bean id="student" class="top.imustctf.pojo.Student">
    <property name="wife">
        <null/>
    </property>
</bean>

Properties注入:

<bean id="student" class="top.imustctf.pojo.Student">
    <property name="info">
        <props>
            <prop key="学号">202099166</prop>
            <prop key="专业">软件工程</prop>
        </props>
    </property>
</bean>

p命名空间注入:

要使用p命名空间,你需要在beans配置头加入如下语句:

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

例如:

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

随后,就可以使用p方式进行注入:

<bean id="student" class="top.imustctf.pojo.Student" p:name="dahe"
          p:wife="xiaoqian"/>

c命名空间注入:

要使用c命名空间,你需要在beans配置头加入如下语句:

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

随后,就可以使用c方式进行注入:(c命名空间是通过构造器进行注入,这就需要pojo类必须存在一个有参的构造方法)

<bean id="student" class="top.imustctf.pojo.Student" c:name="dahe"
          c:wife="xiaoqian"/>
原文地址:https://blog.csdn.net/Gherbirthday0916/article/details/127721258

相关文章: