博客
关于我
Spring依赖注入
阅读量:718 次
发布时间:2019-03-21

本文共 2520 字,大约阅读时间需要 8 分钟。

Spring依赖注入详解

在软件开发中,依赖注入是一种强大的设计模式,能够有效提升代码的可维护性和复用性。尤其在Spring框架中,通过容器管理Bean之间的依赖关系,开发者可以更加专注于业务逻辑的实现,而不是繁琐的依赖管理。

依赖注入概述

在传统的Java程序中,类之间的依赖关系是单向的,通常由调用者负责创建和注入被依赖对象。然而,这种模式会导致类之间的耦合度过高,复杂化和调试困难。在依赖注入(Dependency Injection, DI)模式下,创建被依赖对象的责任被转移到第三方(如Spring容器),通过反转控制(Inversion of Control, IoC)将依赖关系注入到目标对象中。

Spring基于设值函数的依赖注入

这一注入方式通过Spring容器将被依赖对象注入目标Bean的属性字段或方法参数中。常见的方式是使用 setter方法,或者在构造函数中注入。

实例说明

假设有以下两个Bean:

  • AccountDaoImpl

    public class AccountDaoImpl implements AccountDao {    @Override    public void add() {        System.out.println("save account...");    }}
  • AccountServiceImpl

    public class AccountServiceImpl implements AccountService {    private AccountDao accountDao;    public void setAccountDao(AccountDao accountDao) {        this.accountDao = accountDao;    }    @Override    public void addAccount() {        this.accountDao.add();        System.out.println("account added!");    }}
  • 在Spring配置文件中,配置这两个Bean的关系:

    测试与输出

    通过Spring上下文加载Bean:

    public class TestDI_1 {    public static void main(String[] args) {        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");        AccountService accountService = applicationContext.getBean("accountService");        accountService.addAccount();    }}

    运行后,控制台将输出:

    save account...account added!

    Spring基于构造函数的依赖注入

    这种方法通过在构造函数中注入依赖对象,适用于那些依赖关系明确且在构建时需要明确设置的场景。

    实例说明

    改进后的 AccountServiceImpl_2

    public class AccountServiceImpl_2 implements AccountService {    private AccountDao accountDao;    public AccountServiceImpl_2(AccountDao accountDao) {        System.out.println("Inside AccountServiceImpl_2 constructor.");        this.accountDao = accountDao;    }    @Override    public void addAccount() {        System.out.println("account added again!");    }}

    Spring配置文件:

    测试与输出

    通过Spring上下文加载Bean:

    public class TestDI_2 {    public static void main(String[] args) {        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");        AccountService accountService2 = applicationContext.getBean("accountService2");        accountService2.addAccount();    }}

    运行后,控制台将输出:

    Inside AccountServiceImpl_2 constructor.save account...account added again!

    两种注入方式对比

    • 构造注入的优势

      • 适用于明确依赖关系的场景,降低类之间偶合度。
      • 优先注入优先级高的依赖关系,实现更灵活的注入顺序控制。
      • 适合内部化的依赖管理,确保单元测试环境的独立性。
    • 设值注入的优势

      • 操作简便,开发者习惯 setter 方法的方式更为直观。
      • 适用于对依赖关系的可选性需求,对于一些非必需属性的设置更加舒适。
      • 避免了构造函数过长的问题,降低代码可读性。

    选择哪种方式取决于具体的场景需求。无论建立哪种形式,Spring的依赖注入都是提升代码可维护性的高效方式。

    转载地址:http://hjygz.baihongyu.com/

    你可能感兴趣的文章
    MySQL中你必须知道的10件事,1.5万字!
    查看>>
    MySQL中使用IN()查询到底走不走索引?
    查看>>
    Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
    查看>>
    MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
    查看>>
    mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
    查看>>
    mysql中出现Unit mysql.service could not be found 的解决方法
    查看>>
    mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
    查看>>
    Mysql中各类锁的机制图文详细解析(全)
    查看>>
    MySQL中地理位置数据扩展geometry的使用心得
    查看>>
    Mysql中存储引擎简介、修改、查询、选择
    查看>>
    Mysql中存储过程、存储函数、自定义函数、变量、流程控制语句、光标/游标、定义条件和处理程序的使用示例
    查看>>
    mysql中实现rownum,对结果进行排序
    查看>>
    mysql中对于数据库的基本操作
    查看>>
    Mysql中常用函数的使用示例
    查看>>
    MySql中怎样使用case-when实现判断查询结果返回
    查看>>
    Mysql中怎样使用update更新某列的数据减去指定值
    查看>>
    Mysql中怎样设置指定ip远程访问连接
    查看>>
    mysql中数据表的基本操作很难嘛,由这个实验来带你从头走一遍
    查看>>
    Mysql中文乱码问题完美解决方案
    查看>>
    mysql中的 +号 和 CONCAT(str1,str2,...)
    查看>>