iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >详解SpringDI依赖注入的方式和类型
  • 719
分享到

详解SpringDI依赖注入的方式和类型

Spring依赖注入DI依赖注入 2023-05-19 08:05:14 719人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录一、什么是依赖注入二、依赖注入方式1. Setter注入2. 构造方法注入3. 自动注入三、依赖注入类型1. 注入bean类型2. 注入基本数据类型3. 注入List集合4. 注

一、什么是依赖注入

依赖注入(Dependency Injection,简称DI),它是spring控制反转思想的具体实现。 控制反转将对象的创建交给了Spring,但是对象中可能会依赖其他对象。比如service类中要有dao类的属性,我们称service依赖于dao。之前需要手动注入属性值,代码如下:

public interface StudentDao {
  Student findById(int id);
}
public class StudentDaoImpl implements StudentDao{
  @Override
  public Student findById(int id) {
    // 模拟根据id查询学生
    return new Student(1,"程序员","北京");
 }
}
public class StudentService {
   // service依赖dao,手动注入属性值,即手动维护依赖关系
  private StudentDao studentDao = new StudentDaoImpl();
  public Student findStudentById(int id){
    return studentDao.findById(id);
 }
}

此时,当StudentService的想要使用StudentDao的另一个实现类如StudentDaoImpl2时,则需要修改Java源码,造成代码的可维护性降低。

而使用Spring框架后,Spring管理Service对象与Dao对象,此时它能够为Service对象注入依赖的Dao属性值。这就是Spring的依赖注入。简单来说,控制反转是创建对象,依赖注入是为对象的属性赋值

二、依赖注入方式

1. Setter注入

被注入类编写属性的setter方法

    public void setStudentDao(StudentDao studentDao){
        this.studentDao = studentDao;
    }

配置文件中,给需要注入属性值的 <bean> 中设置 <property>

<bean id="studentDao" class="com.itbaizhan.dao.StudentDaoImpl"> </bean>
<bean id="studentService" class="com.itbaizhan.service.StudentService">
  <!--依赖注入-->
  <!--name:对象的属性名 ref:容器中对象的id值-->
  <property name="studentDao" ref="studentDao"></property>
</bean>

测试

新增测试方法

    // 测试依赖注入
    @Test
    public void t6(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        StudentService service = (StudentService) ac.getBean("studentService");
        System.out.println(service.findStudentById(8));
    }

运行结果

OK,确实成功测试到了

2. 构造方法注入

被注入类编写有参的构造方法

    public StudentService(StudentDao studentDao){
        this.studentDao = studentDao;
    }

给需要注入属性值的 <bean> 中设置 <constructor-arg>

<bean id="studentDao" class="com.itbaizhan.dao.StudentDaoImpl"></bean>
<bean id="studentService" class="com.itbaizhan.service.StudentService">
  <!-- 依赖注入 -->
  <!-- name:对象的属性名 ref:配置文件中注入对象的id值 -->
  <constructor-arg name="studentDao" ref="studentDao"></constructor-arg>
</bean>

测试结果:

OK,确实也是可以使用的

3. 自动注入

自动注入不需要在 <bean> 标签中添加其他标签注入属性值,而是自动从容器中找到相应的bean对象设置为属性值。

自动注入有两种配置方式:

  • 全局配置:在 <beans> 中设置 default-autowire 属性可以定义所有bean对象的自动注入策略。
  • 局部配置:在 <bean> 中设置 autowire 属性可以定义当前bean对象的自动注入策略。

autowire的取值如下:

  • no:不会进行自动注入。
  • default:全局配置default相当于no,局部配置default表示使用全局配置
  • byName:在Spring容器中查找id与属性名相同的bean,并进行注入。需要提供set方法。
  • byType:在Spring容器中查找类型与属性类型相同的bean,并进行注入。需要提供set方法。
  • constructor:在Spring容器中查找id与属性名相同的bean,并进行注入。需要提供构造方法。

三、依赖注入类型

DI支持注入bean类型、基本数据类型和字符串、List集合、Set集合、Map集合、Properties对象类型等,他们的写法如下:

准备注入属性的类

package com.example.service;
import com.example.dao.StudentDao;
import com.example.pojo.Student;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class StudentService {
    // service依赖dao,手动注入属性值,即手动维护依赖关系
    //private StudentDao studentDao;
    // bean属性
    private StudentDao studentDao;
    // 字符串类型
    private String name;
    // 基本数据类型
    private int count;
    // 字符串List集合
    private List<String> students1;
    // 对象类型List集合
    private List<Student> nameList;
    // 字符串类型Set集合
    private Set<String> students2;
    // 字符串类型Map集合
    private Map<String, String> students3;
    // 对象类型map集合
    private Map<String,Student> studentMap;
    // Properties类型
    private Properties properties;
    public StudentService(){}
    public StudentService(StudentDao studentDao){
        this.studentDao = studentDao;
    }
    public Student findStudentById(int id){
        return studentDao.findById(id);
    }
    public void setStudentDao(StudentDao studentDao){
        this.studentDao = studentDao;
    }
    public StudentDao getStudentDao() {
        return studentDao;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    public List<String> getStudents1() {
        return students1;
    }
    public void setStudents1(List<String> students1) {
        this.students1 = students1;
    }
    public Set<String> getStudents2() {
        return students2;
    }
    public void setStudents2(Set<String> students2) {
        this.students2 = students2;
    }
    public Map<String, String> getNames2() {
        return students3;
    }
    public void setNames2(Map<String, Student> names2) {
        this.studentMap = names2;
    }
    public Map<String, String> getStudents3() {
        return students3;
    }
    public void setStudents3(Map<String, String> students3) {
        this.students3 = students3;
    }
    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
    public List<Student> getNameList() {
        return nameList;
    }
    public void setNameList(List<Student> nameList) {
        this.nameList = nameList;
    }
    @Override
    public String toString() {
        return "StudentService[ " +
                "studentDao=" + studentDao +
                ", name='" + name + '\'' +
                ", count=" + count +
                ", students1=" + students1 +
                ", nameList=" + nameList +
                ", students2=" + students2 +
                ", students3=" + students3 +
                ", studentMap=" + studentMap +
                ", properties=" + properties +
                " ]";
    }
}

准备测试方法

    // 测试注入类型
    @Test
    public void t7(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        StudentService service = (StudentService) ac.getBean("studentService");
        System.out.println(service);
    }

1. 注入bean类型

    <!-- 注入bean类型 -->
    <bean id="studentDao" class="com.example.dao.StudentDaoImpl1"/>
    <!-- 写法1 -->
    <bean id="studentService" class="com.example.service.StudentService">
        <property name="studentDao" ref="studentDao"/>
    </bean>
    <!-- 写法2 -->
    <!--<bean id="studentService" class="com.example.service.StudentService">
        <property name="studentDao">
            <ref bean="studentDao"/>
        </property>
    </bean>-->

2. 注入基本数据类型

        <!-- 注入基本数据类型 -->
        <!-- 写法一 name:属性名 value:属性值 -->
        <property name="name" value="程序员"/>
        <!-- 写法二 name:属性名 value:属性值-->
        <property name="count">
            <value>10</value>
        </property>

3. 注入List集合

    <!-- 注入List集合 -->
        <!-- 简单的数据类型List集合 name:属性名 -->
        <property name="students1" >
            <list>
                <value>上海</value>
                <value>广州</value>
            </list>
        </property>
        <!-- 对象类型的List集合 name:属性名 -->
        <property name="nameList">
            <list>
                <bean class="com.example.pojo.Student">
                    <property name="id" value="1"/>
                    <property name="name" value="几何心凉"/>
                    <property name="address" value="北京"/>
                </bean>
                <bean class="com.example.pojo.Student">
                    <property name="id" value="2"/>
                    <property name="name" value="哈士奇"/>
                    <property name="address" value="上海"/>
                </bean>
            </list>
        </property>

4. 注入Set集合

        <!-- 注入Set集合 -->
        <property name="students2">
            <set>
                <value>深圳</value>
                <value>北京</value>
            </set>
        </property>

5. 注入Map集合

        <!-- 注入Map集合 -->
        <property name="students3">
            <map>
                <entry key="哈士奇" value="上海"/>
                <entry key="几何心凉" value="北京"/>
            </map>
        </property>
        <!-- 注入对象类型map类型 -->
        <property name="names2">
            <map>
                <entry key="student1" value-ref="s1"/>
                <entry key="student2" value-ref="s2"/>
            </map>
        </property>
    <bean id="s1" class="com.example.pojo.Student">
        <property name="id" value="1"/>
        <property name="name" value="几何心凉"/>
        <property name="address" value="北京"/>
    </bean>
    <bean id="s2" class="com.example.pojo.Student">
        <property name="id" value="2"/>
        <property name="name" value="哈士奇"/>
        <property name="address" value="上海"/>
    </bean>

上面是用到的bean对象

6. 注入Properties对象

        <!-- 注入properties -->
        <property name="properties">
            <props>
                <prop key="配置1">值1</prop>
                <prop key="配置2">值2</prop>
            </props>
        </property>

运行测试方法测试一下

OK ,可以看到都是插入的了。

到此这篇关于详解Spring DI依赖注入的方式和类型的文章就介绍到这了,更多相关Spring DI依赖注入 内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 详解SpringDI依赖注入的方式和类型

本文链接: https://www.lsjlt.com/news/212794.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 详解SpringDI依赖注入的方式和类型
    目录一、什么是依赖注入二、依赖注入方式1. Setter注入2. 构造方法注入3. 自动注入三、依赖注入类型1. 注入bean类型2. 注入基本数据类型3. 注入List集合4. 注...
    99+
    2023-05-19
    Spring 依赖注入 DI依赖注入
  • swift依赖注入和依赖注入容器详解
    目录什么是控制反转(Inversion of Control)?什么是依赖注入?依赖注入的种类初始化器注入属性注入方法注入依赖注入容器实现一个简单的依赖注入容器总结什么是控制反转(I...
    99+
    2023-01-28
    swift依赖注入依赖注入容器 swift依赖注入
  • Java详细讲解依赖注入的方式
    目录Spring中的三种依赖注入方式可能遇到的问题Spring中的三种依赖注入方式 Field Injection :@Autowired注解的一大使用场景就是Field Injec...
    99+
    2024-04-02
  • 详解Angular依赖注入
    目录概述一、依赖注入二、Angular的依赖注入框架概述 依赖注入:设计模式 依赖:程序里需要的某种类型的对象。 依赖注入框架:工程化的框架 注入器Injector:用它的API创...
    99+
    2024-04-02
  • mybatisTypeHandler注入spring的依赖方式
    目录TypeHandler注入spring的依赖解决方法mybatis扩展:自定义TypeHandler1、编写自定义TypeHandler2、配置TypeHandler3、测试4、...
    99+
    2024-04-02
  • ASP.NET Core依赖注入详解
    目录一、什么是依赖注入二、使用框架提供的服务三、注册服务四、生命周期五、请求服务六、设计你的依赖服务ASP.NET Core的底层设计支持和使用依赖注入。ASP.NET Core应用...
    99+
    2024-04-02
  • Spring的DI依赖注入详解
    目录1、什么是DI依赖注入?2、利用 set 方法给属性赋值3、利用 构造函数 给属性赋值总结:1、什么是DI依赖注入? spring动态的向某个对象提供它所需要的其他对象。这一点是...
    99+
    2024-04-02
  • Spring依赖注入(DI)两种方式的示例详解
    目录一、依赖注入方式二、setter注入引用类型简单类型三、构造器注入引用类型简单类型参数适配(了解)四、依赖注入方式选择一、依赖注入方式 思考:向一个类中传递数据的方式有几种? 普...
    99+
    2024-04-02
  • SpringBoot依赖注入的三种方式
    目录SpringBoot依赖注入的三种方式1.使用 XML 配置依赖注入2.使用 Java 配置类实现依赖注入3.使用注解来进行依赖注入SpringBoot依赖注入的三种方式 1.使...
    99+
    2023-05-16
    Java SpringBoot依赖注入 SpringBoot依赖注入方式 SpringBoot依赖注入
  • Vue privide 和inject 依赖注入的使用详解
    目录前言示例项目案例子组件前言 关于Vue组件的通讯方式如下: 父子组件:通过prop,$ emit,【$ root,$ parent,$ children】;非父子组件:vuex,...
    99+
    2022-11-13
    Vue privide 和inject vue 依赖注入
  • 详解PHP设计模式之依赖注入模式
    目的 实现了松耦合的软件架构,可得到更好的测试,管理和扩展的代码 用法 DatabaseConfiguration 被注入 DatabaseConnection 并获取所需的 $co...
    99+
    2024-04-02
  • Java依赖注入的方式是什么
    这篇“Java依赖注入的方式是什么”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Java依赖注入的方式是什么”文章吧。Spr...
    99+
    2023-07-02
  • Spring依赖注入DependencyInjection的三种方式
    目录一、变量注入(Field Injection)二、构造器注入(Constructor Injection)三、setter方法注入(Setter Injection)四、使用场景...
    99+
    2023-02-14
    Spring依赖注入 Spring依赖注入的方式
  • Spring依赖注入的方式有哪些
    这篇文章主要介绍“Spring依赖注入的方式有哪些”,在日常操作中,相信很多人在Spring依赖注入的方式有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Spring依赖注入的方式有哪些”的疑惑有所帮助!...
    99+
    2023-07-02
  • php依赖注入的方式有哪些
    在PHP中,有几种常见的依赖注入的方式:1. 构造函数注入:通过类的构造函数来注入依赖项。这是最常见的依赖注入方式,通过在类的构造函数中声明依赖项,并在创建类的实例时传入相应的依赖项。2. Setter方法注入:通过类的Setter方法...
    99+
    2023-08-11
    php
  • laravel依赖注入的方式有哪些
    在Laravel中,有以下几种方式可以实现依赖注入:1. 构造函数注入:通过在类的构造函数中声明依赖关系,Laravel会自动解析并...
    99+
    2023-09-21
    laravel
  • 详解Spring依赖注入的三种方式以及优缺点
    目录0.概述1.属性注入1.1 优点分析1.2 缺点分析2.Setter 注入优缺点分析3.构造方法注入优点分析总结0.概述 在 Spring 中实现依赖注入的常见方式有以下 3 种...
    99+
    2024-04-02
  • 浅谈spring DI 依赖注入方式和区别
    目录spring DI 3种DI注解的区别 1 @Autowired 2 @Inject 3 @Resource 3种注入方式的区别 1 field注入 2 构造器注入 3 sett...
    99+
    2024-04-02
  • Spring的依赖注入方式有哪几种
    Spring的依赖注入方式有三种: 构造器注入(Constructor Injection):通过在类的构造器中注入依赖对象来实...
    99+
    2024-04-02
  • 使用Spring自定义实现IOC和依赖注入(注解方式)
    目录大致思路:注解实现方式:xml实现方式:1. 引入相关jar2. 定义注解类ExtService是注解类的, ExtResource是注解属性的3.定义一个借口4. 接口和使用注...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作