news 2026/6/10 18:32:36

IOC(控制反转)和DI(依赖注入)详解

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
IOC(控制反转)和DI(依赖注入)详解

一、IOC容器

链接:IOC与DI的详解来源

注意:上个文章里面带过了就不写了

1.这是启动类代码(放在当前启动类所在包下才能扫描完

package com.itheima.springbpptweb01; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // 具备组件扫描的作用 public class SpringbpptWeb01Application { public static void main(String[] args) { SpringApplication.run(SpringbpptWeb01Application.class, args); } }

2.小结

二、DI(依赖注入)

1.基于@Autowired进行依赖注入的常见方式有如下三种:

(1)属性注入(企业快速开发常用的方式)

@RestController // 包装了Conroller public class UserController { //方式一:(快速开发用第一种) @Autowired private UserService userService; @RequestMapping("/list") public List<User> list() throws FileNotFoundException { // 1.调用service,获取数据 List<User> userList = userService.findAll(); //3.返回数据(josn) v return userList; } }

优点:代码简洁、方便快速开发。

缺点:隐藏了类之间的依赖关系、可能会破坏类的封装性。

(2)构造函数注入(要求代码规范性的方式,Spring官方推荐的方法

@RestController // 包装了Conroller public class UserController { 方式二:构造器注入(规范开发用第二种) private final UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } @RequestMapping("/list") public List<User> list() throws FileNotFoundException { // 1.调用service,获取数据 List<User> userList = userService.findAll(); //3.返回数据(josn) v return userList; } }

优点:能清晰的看到类的依赖关系、提高了代码的安全性。

缺点:代码繁琐、如果构造参数过多,可能会导致构造函数臃肿。

注意:如果只有一个构造函数,@Autowired注解可以省略。

(3)setter函数注入

@RestController // 包装了Conroller public class UserController { // 方式三:setter方法注入 private UserService userService; @Autowired public void setUserService(UserService userService) { this.userService = userService; } @RequestMapping("/list") public List<User> list() throws FileNotFoundException { // 1.调用service,获取数据 List<User> userList = userService.findAll(); //3.返回数据(josn) v return userList; } }

优点:保持了类的封装性,依赖关系更清晰。

缺点:需要额外编写setter方法,增加了代码量。

2.DI详解

2.1 先知:

2.2 解决方案

(1) 方案一:@Primary

简化版:

应用版:

@Primary // 默认使用这个提高他的优先级 @Service public class UserServiceImpl implements UserService{ @Autowired // 用多态创建UserDao private UserDao userDao; @Override public List<User> findAll() { // 1.调用dao层方法获取 数据 List<String> list = userDao.findAll(); //2.解析用户信息,封装为user对象 -> list集合 List<User> userList =list.stream().map(line -> { String[] items = line.split(","); Integer id = Integer.parseInt(items[0]); String username = items[1]; String password = items[2]; String name = items[3]; Integer age = Integer.parseInt(items[4]); LocalDateTime updateTime = LocalDateTime.parse(items[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); return new User(id,username,password,name,age,updateTime); }).toList(); return userList; } }
(2)方案二:@Qualifier

简化版:

应用版:

@RestController // 包装了Conroller public class UserController { @Autowired @Qualifier("userServiceImpl") //指定要注入的bean的名字 private UserService userService; @RequestMapping("/list") public List<User> list() throws FileNotFoundException { // 1.调用service,获取数据 List<User> userList = userService.findAll(); //3.返回数据(josn) v return userList; } }
(3)方案三:@Resource

简化版:

应用版:

@RestController // 包装了Conroller public class UserController { @Resource(name="userServiceImpl2") private UserService userService; @RequestMapping("/list") public List<User> list() throws FileNotFoundException { // 1.调用service,获取数据 List<User> userList = userService.findAll(); //3.返回数据(josn) v return userList; } }
(4)注意:
为什么我们要指定名字呢?因为我们在无论是dao,service下都会有一个统一接口,然后写多个实现类去实现它,但是调用的时候不知道要用哪个实现类,所以需要用指定名字。这样就轻松实现了解耦。

3.小结

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 13:58:22

USB转串口Linux驱动编写实战案例解析

从零构建 USB 转串口 Linux 驱动&#xff1a;一次深入内核的实战之旅你有没有遇到过这样的场景&#xff1f;手头有个老旧的 GPS 模块、PLC 控制器或者单片机开发板&#xff0c;只支持 RS232 串口通信。而你的现代笔记本早已砍掉了 COM 口&#xff0c;只剩下几个 USB 接口。这时…

作者头像 李华
网站建设 2026/6/10 13:54:47

NxNandManager深度技术解析:专业级Switch存储管理解决方案

NxNandManager深度技术解析&#xff1a;专业级Switch存储管理解决方案 【免费下载链接】NxNandManager Nintendo Switch NAND management tool : explore, backup, restore, mount, resize, create emunand, etc. (Windows) 项目地址: https://gitcode.com/gh_mirrors/nx/NxN…

作者头像 李华
网站建设 2026/6/9 17:22:03

千兆以太网PHY层PCB布局布线项目应用

千兆以太网PHY层PCB设计实战&#xff1a;从信号完整性到可靠通信的工程之道 在工业控制、边缘计算和智能监控设备中&#xff0c;千兆以太网早已不是“高端配置”&#xff0c;而是系统稳定运行的 基本保障 。但你是否遇到过这样的问题&#xff1a; 板子焊好了&#xff0c;RJ4…

作者头像 李华
网站建设 2026/6/9 20:52:28

SMUDebugTool:5分钟搞定AMD平台硬件调试的终极指南

SMUDebugTool&#xff1a;5分钟搞定AMD平台硬件调试的终极指南 【免费下载链接】SMUDebugTool A dedicated tool to help write/read various parameters of Ryzen-based systems, such as manual overclock, SMU, PCI, CPUID, MSR and Power Table. 项目地址: https://gitco…

作者头像 李华
网站建设 2026/6/10 15:35:21

直播录制神器:DouyinLiveRecorder快速上手终极指南

直播录制神器&#xff1a;DouyinLiveRecorder快速上手终极指南 【免费下载链接】DouyinLiveRecorder 项目地址: https://gitcode.com/gh_mirrors/do/DouyinLiveRecorder 你是否曾经因为错过心爱主播的精彩直播而懊恼不已&#xff1f;想要轻松保存多个平台的直播内容却不…

作者头像 李华