news 2026/10/17 10:18:32

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/10/17 8:16:33

如何快速掌握WeiboSpider:微博数据采集的完整指南

如何快速掌握WeiboSpider&#xff1a;微博数据采集的完整指南 【免费下载链接】WeiboSpider 持续维护的新浪微博采集工具&#x1f680;&#x1f680;&#x1f680; 项目地址: https://gitcode.com/gh_mirrors/weibo/WeiboSpider 想要轻松获取新浪微博的用户信息、推文内…

作者头像 李华
网站建设 2026/10/17 3:54:08

PPTTimer:重新定义演讲时间管理的智能悬浮助手

PPTTimer&#xff1a;重新定义演讲时间管理的智能悬浮助手 【免费下载链接】ppttimer 一个简易的 PPT 计时器 项目地址: https://gitcode.com/gh_mirrors/pp/ppttimer 还在为演讲超时而焦虑吗&#xff1f;想象一下这样的场景&#xff1a;你正在台上激情演讲&#xff0c;…

作者头像 李华
网站建设 2026/10/16 12:37:35

同花顺问财数据获取终极指南:Python自动化工具完整教程

同花顺问财数据获取终极指南&#xff1a;Python自动化工具完整教程 【免费下载链接】pywencai 获取同花顺问财数据 项目地址: https://gitcode.com/gh_mirrors/py/pywencai 想要快速获取A股市场数据却苦于手动查询的繁琐&#xff1f;pywencai正是你需要的Python自动化工…

作者头像 李华
网站建设 2026/10/16 8:16:59

Jellyfin Android TV版自动播放下一集功能失效分析与修复

Jellyfin Android TV版自动播放下一集功能失效分析与修复 【免费下载链接】jellyfin-androidtv Android TV Client for Jellyfin 项目地址: https://gitcode.com/gh_mirrors/je/jellyfin-androidtv 问题背景 在Jellyfin Android TV客户端0.17.7版本更新后&#xff0c;用…

作者头像 李华
网站建设 2026/10/16 11:06:02

烟草专卖执法案卷评查系统:案卷质量效率全域跃升

烟草专卖执法案卷评查工作是保障执法规范性、提升执法公信力的重要环节。随着法治建设的深入推进和案件数量的持续增长&#xff0c;传统人工评查模式已难以适应新形势下的监管需求。北京中烟创新科技有限公司&#xff08;简称&#xff1a;中烟创新&#xff09;为此研发了“烟草…

作者头像 李华