Spring DI简介及依赖注入方式和依赖注入类型
创始人
2024-12-16 22:05:12
0

目录

一、什么是依赖注入

二、依赖注入方式

1. Setter注入

2. 构造方法注入

3. 自动注入 

三、依赖注入类型

1. 注入bean类型

2. 注入基本数据类型

3. 注入List集合

4. 注入Set集合

5. 注入Map集合

6. 注入Properties对象

往期专栏&文章相关导读 

1. Maven系列专栏文章

2. Mybatis系列专栏文章

3. Spring系列专栏文章


一、什么是依赖注入

依赖注入(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;     } 

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

             

测试 

新增测试方法

    // 测试依赖注入     @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;     } 

给需要注入属性值的 中设置

           

测试结果: 

OK,确实也是可以使用的 

3. 自动注入 

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

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

  • 全局配置:在 中设置 default-autowire 属性可以定义所有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 students1;     // 对象类型List集合     private List nameList;     // 字符串类型Set集合     private Set students2;     // 字符串类型Map集合     private Map students3;     // 对象类型map集合     private Map 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 getStudents1() {         return students1;     }      public void setStudents1(List students1) {         this.students1 = students1;     }      public Set getStudents2() {         return students2;     }      public void setStudents2(Set students2) {         this.students2 = students2;     }      public Map getNames2() {         return students3;     }      public void setNames2(Map names2) {         this.studentMap = names2;     }      public Map getStudents3() {         return students3;     }      public void setStudents3(Map students3) {         this.students3 = students3;     }      public Properties getProperties() {         return properties;     }      public void setProperties(Properties properties) {         this.properties = properties;     }      public List getNameList() {         return nameList;     }      public void setNameList(List 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类型

                                           

2. 注入基本数据类型

                                                         10         

3. 注入List集合

                                                    上海                 广州                                                                                                                                                                                                                                                                             

4. 注入Set集合

                                               深圳                 北京                      

5. 注入Map集合

                                                                                                                                                                             
                                                                         

 上面是用到的bean对象

6. 注入Properties对象

                                               值1                 值2                      

运行测试方法测试一下

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

往期专栏&文章相关导读 

     大家如果对于本期内容有什么不了解的话也可以去看看往期的内容,下面列出了博主往期精心制作的Maven,Mybatis等专栏系列文章,走过路过不要错过哎!如果对您有所帮助的话就点点赞,收藏一下啪。其中Spring专栏有些正在更,所以无法查看,但是当博主全部更完之后就可以看啦。

1. Maven系列专栏文章

Maven系列专栏Maven工程开发
Maven聚合开发【实例详解---5555字】

2. Mybatis系列专栏文章

Mybatis系列专栏MyBatis入门配置
Mybatis入门案例【超详细】
MyBatis配置文件 —— 相关标签详解
Mybatis模糊查询——三种定义参数方法和聚合查询、主键回填
Mybatis动态SQL查询 --(附实战案例--8888个字--88质量分)
Mybatis分页查询——四种传参方式
Mybatis一级缓存和二级缓存(带测试方法)
Mybatis分解式查询
Mybatis关联查询【附实战案例】
MyBatis注解开发---实现增删查改和动态SQL
MyBatis注解开发---实现自定义映射关系和关联查询

3. Spring系列专栏文章

Spring系列专栏Spring IOC 入门简介【自定义容器实例】
IOC使用Spring实现附实例详解
Spring IOC之对象的创建方式、策略及销毁时机和生命周期且获取方式
Spring DI简介及依赖注入方式和依赖注入类型
Spring IOC相关注解运用——上篇
Spring IOC相关注解运用——下篇
Spring AOP简介及相关案例
注解、原生Spring、SchemaBased三种方式实现AOP【附详细案例】
Spring事务简介及相关案例
Spring 事务管理方案和事务管理器及事务控制的API
Spring 事务的相关配置、传播行为、隔离级别及注解配置声明式事务

相关内容

热门资讯

分享!微信创建拼三张好友房间/... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享...
科普!微信里面炸金花在哪买房卡... 微信游戏中心:拼三张房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信拼三张房...
一分钟了解“金花房卡在哪获取/... 美猴王牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡...
科普!微信炸金花房卡怎么开房间... 微信游戏中心:拼三张房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信拼三张房...
科普!金花正规房卡总代理,微信... 微信游戏中心:牛牛房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信牛牛房卡”,...
一分钟推荐“金花房卡购买平台推... 炫酷大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来...
分享!微信的牛牛房卡怎么弄/白... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享受...
科普!金花房卡链接跟谁买,微信... 微信游戏中心:拼三张房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信拼三张房...
全能考试系统下载安卓 你有没有想过,考试这个看似枯燥的环节,其实也可以变得有趣又高效呢?没错,今天我要给你介绍一个神器——...
一分钟了解“微信链接金花房卡怎... 战神大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡...
科普!微信玩炸金花哪里买房卡,... 微信游戏中心:炸金花房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信炸金花房...
分享!怎么买炸金花房间链接房卡... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享...
正版授权“微信炸金花房卡链接在... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡...
科普!微信里炸金花房卡卖家联系... 微信游戏中心:炸金花房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信炸金花房卡...
科普!炸金花房卡从哪买的,微信... 微信游戏中心:拼三张房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信拼三张房...
微信斗牛房卡专卖店联系方式/微... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房...
分享!微信链接牛牛房卡怎么买/... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享受...
科普!微信炸金花房卡找谁购买,... 微信游戏中心:拼三张房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信拼三张房...
牛牛链接房卡那里有/炸金花房卡... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...
科普!微信好友炸金花房卡如何购... 微信游戏中心:炸金花房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信炸金花房卡...