当前位置: 首页 > ds >正文

Spring之IoC控制反转

本内容采用最新SpringBoot3框架版本,视频观看地址:B站视频播放

1. IoC控制反转

控制反转(Inversion of Control,IoC)是面向对象编程中的一个设计原则,用来降低程序代码之间的耦合度。

  • 在传统面向对象编程中,获取对象的方式是用new关键字主动创建一个对象,也就是说调用者掌握着对象的控制权。
  • 在使用Spring框架后,对象的实例不再由调用者来创建,而是由Spring容器来创建,Spring容器负责控制程序之间的关系。这样,控制权由调用者转移到Spring容器,控制权发生了反转,这就是Spring的控制反转。

2. DI依赖注入

依赖注入(Dependency Inject,缩写DI)就是由IoC容器在运行期间动态地将某种依赖资源注入对象之中,是Spring框架核心IoC的具体实现。

依赖注入通常有两种实现方式:

  • 构造方法注入,是Spring容器调用构造方法注入被依赖的实例,构造方法可以是有参的或者是无参的。
  • 属性setter方法注入,是Spring最主流的注入方法,在被注入的类中声明一个setter方法,通过setter方法的参数注入对应的值。

3. ApplicationContext接口

BeanFactory接口提供了完整的IoC服务支持,是一个管理bean的工厂,主要负责初始化各种bean,用于访问Spring IoC容器的根接口。

BeanFactory接口的常用方法:

getBean(String name)根据参数名称获取Bean

ApplicationContext是BeanFactory的子接口,也称为应用上下文,由ApplicationContext接口定义。ApplicationContext接口除了包含BeanFactory的所有功能,还添加了对国际化、资源访问、事件传播等内容的支持。因此,通常建议使用ApplicationContext接口初始化Spring IoC容器。

ClassPathXmlApplicationContext从src路径加载XML文件,实例化ApplicationContext接口

4. 基于xml配置的setter方法注入案例

通过setter方法方式来演示Spring容器在应用中是如何实现依赖注入的,实现StudentService调用StudentDao的saveStudent操作。

4.1 创建Java项目

 Idea创建Java项目,项目名称为:spring-student01。

4.2 导入Spring核心Jar包

spring-student01项目下创建lib目录,在lib目录下导入Jar包:        

核心包:

  • spring-core-6.0.0-RC2.jar、                      
  • spring-beans-6.0.0-RC2.jar、                      
  • spring-context-6.0.0-RC2.jar、                    
  • spring-expression-6.0.0-RC2.jar        

依赖包:spring-jcl-6.0.0-RC2.jar        

测试包:junit-4.6.jar

4.3 创建StudentDao类

com.wfit.dao目录下创建StudentDao.java类

public class StudentDao {public void saveStudent(){System.out.println("保存学生信息成功!");}}

4.4 创建StudentService类

com.wfit.service目录下创建StudentService类,实现addStudent方法。

public class StudentService {private StudentDao studentDao;public void setStudentDao(StudentDao studentDao) {this.studentDao = studentDao;}public void addStudent(){studentDao.saveStudent();}}

4.5 创建Spring配置文件

src目录下创建applicationContext.xml配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--将StudentDao类配置给Spring,让Spring创建其实例--><bean id="studentDao" class="com.wfit.dao.StudentDao"/><!--StudentService类配置给Spring,让Spring创建其实例--><bean id="studentService" class="com.wfit.service.StudentService"><!--将studentDao实例注入到了studentService中--><property name="studentDao" ref="studentDao"/></bean>
</beans>

4.6 创建测试类

com.wfit目录下创建TestStudent测试类。

public class TestStudent {@Testpublic void test(){//初始化Spring容器ApplicationContext,加载配置文件ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");//通过容器获取StudentService实例StudentService studentService = (StudentService)applicationContext.getBean("studentService");studentService.addStudent();  //调用addStudent方法}
}

4.7 执行测试

在IDEA中启动TestStudent测试类,控制台会输出结果。

http://www.xdnf.cn/news/2918.html

相关文章:

  • 【Maven】子POM与父POM
  • C++23/26 静态反射机制深度解析:编译时元编程的新纪元
  • 一文读懂布隆过滤器:特性、应用与局限
  • docker存储
  • 在g2o图优化框架中,顶点(Vertex)和边(Edge)的定义与功能的区别
  • 基于Python镜像创建docker镜像时pip install一直出现NewConnectionError的一种解决办法
  • AGV、AMR机器人控制器x86/RK3588/NV各有什么优劣势?
  • 【Stable Diffusion】使用教程:从原理到实战,全面掌握AI绘画
  • VMware安装Ubuntu实战分享
  • 白光干涉技术在高精度表面形貌测量中的实际应用
  • 永磁同步电机控制算法-转速环电流环SMC控制器
  • 漫反射实现+逐像素漫反射+逐像素漫反射实现
  • 机器学习分类模型性能评估:应对类别不平衡的策略与指标
  • 数据结构 RBT 插入操作的 Python 代码实现
  • EMB量产首航!炯熠电子引领「线控底盘革命」
  • SOLIDWORKS修改模型默认颜色教程
  • Unity AI-使用Ollama本地大语言模型运行框架运行本地Deepseek等模型实现聊天对话(一)
  • WebXR教学 06 项目4 跳跃小游戏
  • for(auto it: vec)和for(auto it: vec)的区别以及使用场景
  • Java—— Arrays工具类及Lambda表达式
  • 联合体union的特殊之处
  • 软件测试实验报告3 | 自动化测试工具的基本操作
  • 局域网传文件——基于flask实现
  • 9.Three.js中 ArrayCamera 多视角相机详解+示例代码
  • RISCV学习(5)GD32VF103 MCU架构了解
  • 修改Hosts文件没有生效的解决办法
  • LM393比较器的比较翻转电压不对
  • seaborn数据统计可视化-介绍
  • 需要掌握的前端安全概念以及实操
  • 【React Native】精通 react native