SSM框架之Spring的DI依赖注入(详细使用案例)

15 篇文章 0 订阅
9 篇文章 0 订阅
本文详细介绍了Spring框架中的依赖注入(DI)概念,包括通过setter方法和构造器进行注入,并展示了不同类型的注入,如基本类型、对象、集合、Map和数组。此外,还探讨了自动注入的两种方式:按名字和按类型注入,以及通过注解实现的注入。文章以实例代码的形式,辅助理解DI的实现和应用。
摘要由CSDN通过智能技术生成

目录

一、什么是DI依赖注入

二、注入的方式

1.set

 2.构造

三、注入的类型

四、自动注入 

1.根据名字注入

 2.根据类型注入

 五、通过注解注入


一、什么是DI依赖注入

  • 依赖注入(Dependency Injection,DI)。

  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .

  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .

二、注入的方式

1.set

创建一个userdao接口添加一个sleep方法

 并创建student和teacher类来实现userdao

package com.exy.dao;

public interface UserDao {
    void sleep();
}
package com.exy.dao;

/**
 * @program: spring03
 * @description:
 * @author: jdy
 * @create: 2021-12-08 15:13
 **/

public class StudentUserDao implements UserDao{
    public void sleep() {
        System.out.println("学生正在睡觉!!!");
    }
}
package com.exy.dao;

/**
 * @program: spring03
 * @description:
 * @author: jdy
 * @create: 2021-12-08 15:14
 **/

public class TeacherUserDao implements UserDao{
    public void sleep() {
        System.out.println("老师正在睡觉!!!!!");
    }
}

添加service类并设置相关的属性及方法 在这里面设置一个userdao的set方法

package com.exy.service;
import com.exy.dao.UserDao;

/**
 * @program: spring03
 * @description:
 * @author: jdy
 * @create: 2021-12-08 15:23
 **/

public class UserService {
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void sleep(){
        userDao.sleep();
    }

}

 配置spring.xml 

        <bean id="stu" class="com.exy.dao.StudentUserDao"/>
        <bean id="tea" class="com.exy.dao.TeacherUserDao"/>
        <bean id="service" class="com.exy.service.UserService">
                <property name="userDao" ref="stu"/>
        </bean>

 创建测试类

package com.exy.test;

import com.exy.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @program: spring03
 * @description:
 * @author: jdy
 * @create: 2021-12-08 15:41
 **/

public class Test {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("spring.xml");
        UserService service = (UserService) app.getBean("service");
        service.sleep();
    }

}

 运行结果如下

 同理更改spring.xml中的ref

 运行结果如下

 2.构造

类不是你定义,而是使用别人的类,而别人写的类没有无参构造函数。

一般很少使用

在service类中加入对userdao的构造

public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

 配置spring.xml


<!--通过构造方法为类中属性注入:参数的下标-->
<!--<constructor-arg index="0" ref="stu"/>-->
<!--通过构造方法为类中属性注入:参数名称-->
<!--<constructor-arg name="userDao" ref="stu"/>-->
<!--通过构造方法为类中属性类型注入:参数名称-->(不推荐)

三、注入的类型

(1)基本数据类型或者字符串。

(2)引用类型--对象类型。

(3)集合List,Set。

(4)map集合。

(5)数组

<?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="clazz01" class="com.exy.di.Clazz">
        <property name="id" value="145"/>
        <property name="name" value="qy145"/>
    </bean>

    <bean id="stu" class="com.exy.di.Student">
        <property name="age" value="18"/>
        <property name="arr">
            <array>
                <value>110</value>
                <value>120</value>
                <value>140</value>
                <value>130</value>
            </array>
        </property>
        <property name="clazz" ref="clazz01"/>
        <property name="list">
            <list>
                <value>1</value>
                <value>1</value>
                <value>1</value>
                <value>1</value>
            </list>
        </property>

        <property name="map">
            <map>
                <entry key="1" value="2"/>
                <entry key="1" value="2"/>
                <entry key="1" value="2"/>
                <entry key="1" value="2"/>
            </map>
        </property>

        <property name="name" value="110"/>
        <property name="set">
            <set>
                <value>1</value>
                <value>1</value>
                <value>13</value>
                <value>12</value>

            </set>
        </property>
    </bean>
</beans>

测试

package com.exy;

import com.ykq.di.Student;
import com.ykq.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test04 {
    public static void main(String[] args) {
        ApplicationContext app=new ClassPathXmlApplicationContext("spring04.xml");

        Student stu = (Student) app.getBean("stu");

        System.out.println(stu);

    }
}

 

 结果如图

四、自动注入 

1.根据名字注入

 

 

 

UserDao的名字要和xml中id的一致

测试结果如下

 2.根据类型注入

xml中只能存在已在对应的类型

存在那个即对应的就是哪一个

 

 五、通过注解注入

在配置文件中加入包扫描

 

 按照类型注入 byType  如果类型有多个 则按照名称注入

 

 结果如下

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值