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

Spring的IOC/DI,依赖注入的实现

Spring的IOC/DI,依赖注入的实现

https://download.csdn.net/download/weixin_41957626/87546826 资源地址

 

1.什么是Spring

1.1spring3 的体系结构图

图1 spring3的体系结构图

图2 spring4体系结构图

比较spring3的体系结构图,spring4去掉了spring3中的struts模块,添加了messaging模块和websocket模块,其他模块保持不变。spring的jar包有20个。

1.下面是spring4和5的区别?

在增强开发方面spring4是web开发增强,spring5是JDK8的增强。

在特性改进方面spring4是注解、脚本、任务、MVC等其他特性改进,spring5是测试方面的改进。

2.IOC/DI

2.1 IOC/DI

1.控制反转或者是依赖注入的含义。面向接口的编程。

2.实现方式

  • 采用工厂类的方式
  • 采用spring的ioc实现

2.2Spring的配置

1.依赖导入

<!--        spring依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.24</version>
        </dependency>

2.创建配置文件

<?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"><!--    配置bean
id指定的是bean的名称
class指定的是bean所在的地址
ref指定的是要引入的bean
property设置的是bean的依赖的项,ref采用的是引用的数据类型的方式,value是赋一般的值的形式
-->
    <bean id="studentDao" class="cn.lxz.dao.impl.StudentDaoImpl"/>
    <bean id="studentService" class="cn.lxz.service.impl.StudentServiceImpl">
        <property name="studentDao" ref="studentDao"/>
    </bean></beans>

3.项目目录

图3 项目目录

4.实现的代码

采用的是gif的形式展示

 

图4 spring代码展示

测试类代码

    @Test
    public void testContext(){
        //采用的是读取xml的文件的配置形式
        ApplicationContext context= new ClassPathXmlApplicationContext("Spring-context.xml");
        StudentService service1=   context.getBean(StudentService.class);
        StudentService service2= (StudentService)context.getBean("studentService");
        System.out.println(service1);
        System.out.println(service2);
        service1.addStudent(new Student(1,"张三"));
    }

图5 效果图

http://www.lryc.cn/news/34490.html

相关文章:

  • 【tensorflow onnx】TensorFlow2导出ONNX及模型可视化教程
  • 天梯赛训练L1-013--L1-015
  • 进程(操作系统408)
  • 浅谈运维工程师的开发能力的培养
  • Netcode升级到1.2.0网络变量的变化的变化
  • 冥想第七百二十二天
  • AB测试——流程介绍(定义问题和指标选取)
  • Linux(Centos)安装Minio集群
  • LeetCode 1662. 检查两个字符串数组是否相等 / 795. 区间子数组个数 / 剑指 Offer 47. 礼物的最大价值
  • 【C++】缺省参数函数重载
  • Hbuilder 下载与安装教程
  • Mybatis工程升级到FlunetMybatis后引发的问题以及解决方法
  • Oracle VM VirtualBox6.1.36导入ova虚拟机文件报错,代码: E_INVALIDARG (0x80070057)
  • Superset数据探索和可视化平台入门以及案例实操
  • VisualSP Enterprise - February crack
  • 004+limou+HTML——(4)HTML表格
  • uniapp实现自定义相机
  • 插值多项式的龙格现象的介绍与模拟
  • Spring整体架构包含哪些组件?
  • 开发接口需要考虑哪些问题?
  • 关于Activiti7审批工作流绘画流程图(2)
  • String.format()对日期进行格式化
  • 核酸检测信息管理系统
  • 典型回溯题目 - 全排列(一、二)
  • 数据清洗和特征选择
  • java StringBuilder 和 StringBuffer 万字详解(深度讲解)
  • 【Linux】帮助文档查看方法
  • UEFI 实战(2) HelloWorld 之一 helloworld及.inf文件
  • 向2022年度商界木兰上榜女性致敬!
  • ChatGPT助力校招----面试问题分享(二)