依赖注入(Dependency Injection)

在当前类需要用到其他的类时,由Spring为我们提供,我们只需要在配置文件中说明依赖关系的维护,这就称为依赖注入。

能注入的三类

  1. 基本类型和String
  2. 其他bean类型(在配置文件中或注解中配置过的bean)
  3. 复杂类型/集合类型

注入的方式

  1. 使用构造函数

    有如下类,拥有一个有参的构造函数

    package com.oylong.service.impl;
    
    import com.oylong.service.AccountService;
    
    import java.util.Date;
    
    public class AccountServiceImpl implements AccountService {
    
        private String str;
        private Integer integer;
        private Date date;
    
        public AccountServiceImpl(String str, Integer integer, Date date) {
            this.str = str;
            this.integer = integer;
            this.date = date;
        }
    
        public void saveAccount() {
            System.out.println("account service");
        }
    }

    在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="accountService" class="com.oylong.service.impl.AccountServiceImpl">
            <constructor-arg name="str" value="test"></constructor-arg>
            <constructor-arg name="integer" value="10"></constructor-arg>
            <constructor-arg name="date" ref="now"></constructor-arg>
        </bean>
        <bean id="now" class="java.util.Date"></bean>
    </beans>

    优势:在获取对象时,必须注入数据,否则对象不能创建成功

    弊端:改变了bean的实例化方式,在我们创建对象时,即使用不到的属性,也必须提供,因此此方法适用较少

    可以看我我们使用了constructor-arg这个标签,它的属性有这些:

    • type:用于指定要注入数据的数据类型,该数据类型也是构造函数中的某个或者某些值的类型
    • index:用于指定要注入的数据在构造函数中的索引位置,从0开始
    • name:用于指定给构造函数中指定的参数赋值,用得最多
    • value:要赋的值
    • ref:用于引用当前已经存在的bean类型
  2. 使用set方法注入

    • 在实体类中创建set方法
    public class AccountServiceImpl implements AccountService {
    private String str;
    private Integer integer;
    private Date date;
    
    public void setStr(String str) {
        this.str = str;
    }
    
    public void setInteger(Integer integer) {
        this.integer = integer;
    }
    
    public void setDate(Date date) {
        this.date = date;
    }
    • bean配置
      <bean id="accountService" class="com.oylong.service.impl.AccountServiceImpl">
        <property name="str" value="name"></property>
        <property name="date" ref="now"></property>
        <property name="integer" value="22"></property>
    </bean>
    • 可以看到,我们使用了property标签,它有的3个属性,与上面的方法相似,但是需要注意的是name属性,Spring是通过name去找到相对应属性的set方法,将要实例化的对象的属性通过set方法赋值,且Spring会将存在的set+属性名方法中,属性名单独抽取出来作为name属性,且为小写。
    • 它的优势与劣势与使用构造函数的方法相反
  3. 使用注解注入

    之后学习......

复杂类型的注入

如下配置文件

<bean id="accountService" class="com.oylong.service.impl.AccountServiceImpl">
    <property name="list">
        <list>
            <value>哈哈</value>
            <value>呵呵</value>
        </list>
    </property>

    <property name="map">
        <map>
            <entry key="哈哈哈" value="呵呵呵"></entry>
            <entry key="呵呵呵" value="哈哈哈"></entry>
        </map>
    </property>

    <property name="properties">
        <props>
            <prop key="haha">呵呵呵</prop>
            <prop key="hehe">哈哈哈</prop>
        </props>
    </property>

</bean>

其中,Set,Array,List的用法类似,直接使用相应的标签,添加value即可,map需要用到entry标签,并且配置keyvalueproperties使用props标签后,再添加prop标签,key填写属性,中间的内容填写属性值.

最后修改:2020 年 03 月 26 日
如果觉得我的文章对你有用,请随意赞赏