Spring IoC——DI依赖注入

简介: DI(Dependency Injection)就是在使用Spring框架创建对象时,动态的将其所依赖的对象注入Bean组件中,而Spring容器实现属性注入的方式有三种 :setter方法注入、 构造方法注入、接口注入(不常用)

1.setter方法注入(常用)

在bean标签中通过配置property标签来给对象属性赋值,本质就是通过调用无参构造器实例化Bean后,调用该Bean的setter方法。
public class Student {
    private int id;
    private String name;
    private String sex;
    private Date date;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Set<String> games;
    private Map<String, String> card;
    private String graduate;
    private Properties properties;
    
    //为每个属性生成对应的setter/getter方法
}
public class Address {
    private String address;
}

1.1 基本类型

<bean id="date" class="java.util.Date"/>

<bean id="student" class="com.ioc.pojo.Student">
    <!--简单类型-->
    <property name="id" value="101"/>
    <property name="name" value="tom"/>
    <property name="sex" value="男"/>
</bean>

1.2 日期类型

  • 方式一:
<bean id="date" class="java.util.Date"/>

<bean id="student" class="com.ioc.pojo.Student">
    <!--值的引用-->
    <property name="date" ref="date"/>
</bean>
  • 方式二:
<bean id="student" class="com.ioc.pojo.Student">
    <!--通过子标签bean来赋值-->
    <property name="date">
        <bean class="java.util.Date"/>
    </property>
</bean>

1.3 自定义类的对象类型

<bean id="address" class="com.ioc.pojo.Address">
        <property name="address" value="上海"/>
</bean>
<bean id="student" class="com.ioc.pojo.Student">
    <!--值的引用-->
    <property name="address" ref="address"/>
</bean>

1.4 数组类型

<bean id="student" class="com.ioc.pojo.Student">
<!--数组注入-->
<property name="books">
    <array>
        <value>Java基础</value>
        <value>数据结构</value>
        <value>数据库</value>
        <value>操作系统</value>
    </array>
</property>
</bean>

1.5 集合类型

  • List
<bean id="student" class="com.ioc.pojo.Student">
    <property name="hobbies">
        <list>
            <value>篮球</value>
            <value>足球</value>
            <value>网球</value>
        </list>
    </property>
</bean>
  • Set
<bean id="student" class="com.ioc.pojo.Student">
    <property name="games">
        <set>
            <value>英雄联盟</value>
            <value>王者荣耀</value>
            <value>我的世界</value>
        </set>
    </property>
</bean>
  • Map
<bean id="student" class="com.ioc.pojo.Student">
    <property name="card">
        <map>
            <entry key="学号" value="123456"/>
            <entry key="身份证号" value="511254200001012589"/>
        </map>
    </property>
</bean>

1.6 注入Null

<bean id="student" class="com.ioc.pojo.Student">
    <property name="graduate">
        <null/>
    </property>
</bean>

1.7 Properties

<bean id="student" class="com.ioc.pojo.Student">
    <!--Properties注入 key,value只能是String类型-->
    <property name="properties">
        <props>
            <prop key="年级">三年级</prop>
            <prop key="班级">二班</prop>
        </props>
    </property>
</bean>

2.构造方法注入

通过调用带参数的构造方法来实现,每个参数代表一个依赖。

2.1 简单类型、引用类型

public class Student {

    private int id;
    private String name;
    private String sex;
    private Date date;

    public Student(int id, String name, String sex, Date date) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.date = date;
    }
}
<bean id="date" class="java.util.Date"/>
<bean id="student" class="com.ioc.pojo.Student">
<!--赋值顺序与对象属性顺序一致时,可以不加index-->
        <constructor-arg index="0" value="101"/>
        <constructor-arg index="1" value="tom"/>
        <constructor-arg index="2" value="男"/>
        <constructor-arg index="3" ref="date"/>
    </bean>

2.2 数组、集合、Properties

public class Student {

    private String[] books;
    private List<String> hobbies;
    private Set<String> games;
    private Map<String, String> card;
    private Properties properties;

    public Student(String[] books, List<String> hobbies, Set<String> games, Map<String, String> card, Properties properties) {
        this.books = books;
        this.hobbies = hobbies;
        this.games = games;
        this.card = card;
        this.properties = properties;
    }
}
<bean id="stu1" class="com.ioc.pojo.Student">
    <constructor-arg index="0">
        <array>
            <value>Java基础</value>
            <value>数据结构</value>
            <value>数据库</value>
            <value>操作系统</value>
        </array>
    </constructor-arg>
    <constructor-arg index="1">
        <list>
            <value>篮球</value>
            <value>足球</value>
            <value>网球</value>
        </list>
    </constructor-arg>
    <constructor-arg index="2">
        <set>
            <value>LOL</value>
            <value>COC</value>
            <value>BOB</value>
        </set>
    </constructor-arg>
    <constructor-arg index="3">
        <map>
            <entry key="key1" value="spring"/>
            <entry key="key2" value="springMVC"/>
        </map>
    </constructor-arg>
    <constructor-arg index="4">
        <props>
            <prop key="1">123</prop>
            <prop key="2">456</prop>
        </props>
    </constructor-arg>
</bean>

总结

今天主要介绍了依赖注入的两种方式,接口注入现在已基本不用就没有介绍,当然最常用的还是setter方式注入,希望这篇文章可以帮到大家!

相关文章
|
8月前
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
518 26
|
5月前
|
XML Java 数据格式
Spring IoC容器的设计与实现
Spring 是一个功能强大且模块化的 Java 开发框架,其核心架构围绕 IoC 容器、AOP、数据访问与集成、Web 层支持等展开。其中,`BeanFactory` 和 `ApplicationContext` 是 Spring 容器的核心组件,分别定位为基础容器和高级容器,前者提供轻量级的 Bean 管理,后者扩展了事件发布、国际化等功能。
|
10月前
|
XML Java 数据格式
【SpringFramework】Spring IoC-基于XML的实现
本文主要讲解SpringFramework中IoC和DI相关概念,及基于XML的实现方式。
198 69
|
7月前
|
Java 容器 Spring
什么是Spring IOC 和DI ?
IOC : 控制翻转 , 它把传统上由程序代码直接操控的对象的调用权交给容 器,通过容器来实现对象组件的装配和管理。所谓的“控制反转”概念就是对组件对象控制权的转 移,从程序代码本身转移到了外部容器。 DI : 依赖注入,在我们创建对象的过程中,把对象依赖的属性注入到我们的类中。
|
10月前
|
设计模式 XML Java
【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
本文详细介绍了Spring框架的核心功能,并通过手写自定义Spring框架的方式,深入理解了Spring的IOC(控制反转)和DI(依赖注入)功能,并且学会实际运用设计模式到真实开发中。
【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
|
10月前
|
Java Spring 容器
【SpringFramework】Spring IoC-基于注解的实现
本文主要记录基于Spring注解实现IoC容器和DI相关知识。
146 21
|
10月前
|
存储 Java 应用服务中间件
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
235 12
|
XML 数据格式 Java
Spring_DI_XML_01
欢迎移步博客查看-http://futaosmile.oschina.io/coder957 基于XMl的DI 1.设值注入 2.构造注入 3.p命名空间设值注入 4.
874 0
|
XML 数据格式 Python
Spring_DI_XML_02
欢迎移步博客查看-http://futaosmile.oschina.io/coder957 基于XMl的DI 1.集合属性注入 2.array数组属性注入 3.
927 0
|
XML Java 数据格式
Spring_DI_XML_03
欢迎移步博客查看-http://futaosmile.oschina.io/coder957 基于XMl的DI 匿名Bean 匿名内部Bean 同类抽象Bean 异类抽象Bean 多个配置文件-平行关系 多个配置文件-包含关系 Java中的匿名内部类 匿名Bean 没有id,但是可以通过autowire="byType"找到。
794 0