【关于Spring那些事】 —— 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方式注入,希望这篇文章可以帮到大家!

猜你喜欢

转载自blog.csdn.net/weixin_52986315/article/details/124154520