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

Java SpringMVC(三)--- SpringMVC,SpringIoCDI

文章目录

  • SpringMVC
    • 应用分层
    • 企业的命名规范
    • 总结
  • Spring IoC详解
    • DI
    • 总结
    • DI详解
    • @Controller(表现层存储)
    • @Service(业务逻辑存储)
    • @Repository(仓库存储)
    • @Component(组件存储)
    • @Configuration(配置存储)

SpringMVC

应用分层

在这里插入图片描述

  1. MVC和三层架构之间是什么样的关系?
    MVC模式强调数据和视图分离
    三层架构是不同维度的数据处理(业务逻辑处理,数据处理,表现层处理数据的校验和数据的返回)
    MVC是一种三层架构的实现
    三层架构是MVC的一种替代
    MVC是一种思想,三层架构是一种实现

  2. 软件的设计原则:高内聚低耦合
    高内聚低耦合:指的是一个模块之内是要高内聚的(关系紧密),模块与模块之间是要低耦合的(它们之间的影响不要那么大),比如A要调用B,B改了一个代码,A就跟着改,这就是高耦合
    在这里插入图片描述

  3. MVC和三层架构的共同目的都是为了解耦,分层和代码复用

  4. 应用分层的好处:
    在这里插入图片描述

企业的命名规范

  1. 类名,变量名,数据库中的命名,串形Java中很少用到(在前端中比较常用到)
    在这里插入图片描述
    在这里插入图片描述

总结

  1. SpringBoot和SpringMVC之间的关系?
    使用SpringBoot创建SpringMVC框架(SpringMVC是一个web框架)

Spring IoC详解

在这里插入图片描述

  1. Spring是一个包含众多工具的IoC容器
  2. Spring两大核心思想:
    (1) IoC
    (2) AOP
    这两个是重要的面试题
  3. 对于Spring是IoC容器的理解:
    在这里插入图片描述
    控制反转的例子:比如一个公司要裁员,公司老板把权力交给了hr,hr就可以进行裁员了

写注解就是为了把控制权交给Spring,让Spring进行对象的管理

  1. IoC的目的也是为了解耦:
    举一个耦合性高的例子:
    在这里插入图片描述
    在这里插入图片描述
    ioc如何进行解耦呢?
    全都在Main这个类中,new很多个需要的对象,然后把这些对象传给后面的类使用,后面的类各自封装好这些类,它们之间只需要传这些封装好的类即可,不需传Main中的对象了

Main

package com.example.ioc.demos.ioc.v2;public class Main {public static void main(String[] args) {int size = 19;String color = "red";int price = 100;Tire tire = new Tire(19,color);Bottom bottom = new Bottom(tire,100);Framework framework = new Framework(bottom);Car car = new Car(framework);car.run();}
}

Car

package com.example.ioc.demos.ioc.v2;public class Car {private Framework framework;// 之前是自己去new的,现在不再new了// 现在是你给我提供一个,我就用你提供给我的这个public Car(Framework framework){this.framework = framework;System.out.println("Car init...");}public void run(){System.out.println("car run...");}
}

Framework

package com.example.ioc.demos.ioc.v2;public class Framework {private Bottom bottom;public Framework(Bottom bottom) {this.bottom = bottom;System.out.println("Framework init...");}
}

Bottom

package com.example.ioc.demos.ioc.v2;public class Bottom {private Tire tire;private int price;public Bottom(Tire tire,int price) {this.tire = tire;this.price = price;System.out.println("Bottom init..." + ",price: " + price);}
}

Tire

package com.example.ioc.demos.ioc.v2;public class Tire {private int size;public String color;public Tire(int size,String color){this.size = size;this.color = color;System.out.println("tire init..." + size + " + color: " + color);}
}

Spring 帮我们管理对象:
1.告诉Spring,帮我们管理哪些对象(存)
2.知道如何取出来这些对象(取)

DI

  1. IoC是一种控制反转(把你的权力交给别人来行使,达到你要的结果)的思想,有很多种方式可以实现IoC,这里使用DI来实现IoC
    在这里插入图片描述
  2. 什么是依赖注入?
    DI是依赖注入,比如第一个类依赖于第二个类,那么可以把第二个类注入到第一个类中

下面这个图,就是依赖注入的具体实现:
在这里插入图片描述

总结

  1. 对上面内容的总结:
    Spring包括了Spring家族
    Spring中有Spring Core,Spring Framework等
    (1) Spring,SpringMVC,SpringBoot区别?
    Spring是一个管理对象的容器,Spring是包含众多工具的IoC容器,Spring存的是对象,对象这个词,在Spring的范围内,称为bean
    SpringMVC是Spring下的一个web框架,使用SpringBoot可以更加快速的创建Spring
    在这里插入图片描述
    在这里插入图片描述

(2) 什么是IoC?
IoC是控制权的反转,将控制权交给别人(控制反转)
优点:
1.解耦,使各个模块之间的影响要小
2.对象的资源可以共享

IoC是思想,DI是一种实现方式
IoC:依赖对象的创建的控制权交给Spring来管理(存)
DI:依赖注入,DI就是把依赖对象取出来,并赋给对象的属性(取)
DI就是使用注解进行依赖注入

(3) 软件的设计原则:高内聚,低耦合
高内聚:一个模块内部的关系要紧密
低耦合:各个模块之间的影响要小

(4) 以前的Spring问题:配置多且繁琐,现在有了SpringBoot帮我们配置文件

DI详解

  1. IoC就是用来存的,IoC共有两种注解类型可以实现:

context也叫做容器
在这里插入图片描述
类注解可以把这些类存入容器中,可以使用容器来使用这些类
2. 有三种方法可以获取存入容器中的对象:
在这里插入图片描述

@Controller(表现层存储)

@Controller:Spring用来存储类,然后取出来获取类的对象
3. @Controller注解:
在这里插入图片描述
IocApplication.java

package com.example.ioc;import com.example.ioc.demos.controller.UserController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;// 启动类
@SpringBootApplication
public class IocApplication {public static void main(String[] args) {// 这条代码帮我们返回了一个容器// Spring上下文ApplicationContext context = SpringApplication.run(IocApplication.class, args);UserController bean = context.getBean(UserController.class);bean.doController();}}

UserController.java

package com.example.ioc.demos.controller;import org.springframework.stereotype.Controller;@Controller
public class UserController {public void doController(){System.out.println("doController...");}
}

Spring存对象时,名称是唯一的

@Service(业务逻辑存储)

@Service:Spring用来存储类,然后取出来获取类的对象

  1. 三种获取bean的方式,还有一种特殊情况(如果类名的前两个字母都大写,那么获取bean的名称就是类名)
    Spring中文文档
package com.example.ioc;import com.example.ioc.demos.controller.UserController;
import com.example.ioc.demos.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;// 启动类
@SpringBootApplication
public class IocApplication {public static void main(String[] args) {// 这条代码帮我们返回了一个容器// Spring上下文// 从context中获取beanApplicationContext context = SpringApplication.run(IocApplication.class, args);UserController bean = context.getBean(UserController.class);bean.doController();// 1.根据类名来获取beanUserService bean1 = context.getBean(UserService.class);bean1.doService();// 2. 根据名称来获取bean,名称是类名,但是是使用小驼峰的方式来命名// 要进行类型的转换,因为getBean是Object类型的UserService bean2 = (UserService) context.getBean("userService");bean2.doService();// 3. 根据名称和类名拿到bean,可能根据类名拿到多个对象UserService userServiece = context.getBean("userService", UserService.class);userServiece.doService();}}

bean内部的实现方法:
在这里插入图片描述
一个经典的面试题:

在这里插入图片描述

@Repository(仓库存储)

持久层,存在数据库中的
格式化的快捷键:ctrl + alt + l(小写的L)

IocApplication.java

// RepositoryUserRepository repository = context.getBean(UserRepository.class);repository.doRepository();

UserRepository.java

package com.example.ioc.demos.repo;import org.springframework.stereotype.Repository;@Repository
public class UserRepository {public void doRepository(){System.out.println("do Repository...");}
}

@Component(组件存储)

IocApplication.java

// ComponentUserComponent userComponent = context.getBean(UserComponent.class);userComponent.doComponent();

UserComponent.java

package com.example.ioc.demos.component;import org.springframework.stereotype.Component;@Component
public class UserComponent {public void doComponent(){System.out.println("doComponent");}
}

@Configuration(配置存储)

  1. Spring会帮我们配置大多数很普遍遇到的配置,但是小众的配置不会帮我们配置,所以引入了@Configuration

  2. 为什么要这么多的注解?
    在这里插入图片描述

  3. 程序的应用分层,调用流程如下:
    在这里插入图片描述

  4. 如果想要被外界(网络)访问到,只能使用Controller

  5. 几个注解之间的关系:
    在这里插入图片描述

http://www.dtcms.com/a/473808.html

相关文章:

  • 网站建设的验收网站上如何放入地图
  • Java 开发工具,最新2025 IDEA 使用,保姆级教程
  • 内网穿透~
  • 【Java EE进阶 --- SpringBoot】Mybatis操作数据库(基础二)
  • 【ComfyUI】Flux 扩展原始图像边界
  • 068B-基于R语言平台Biomod2集成模型的物种分布模型构建和数据可视化教程【2027】
  • Custom SRP 12 - HDR
  • 偏振相机是否属于不同光谱相机的范围内
  • 烟台房地产网站建设视频直播服务
  • SQL Server中alter对于表的常用操作
  • 学校网站建设报告九江 网站建设公司
  • Blender图片转3D模型智能插件 True Depth V2附使用教程
  • 【数据分享】中国土地利用数据(1980-2015)
  • 工信部网站备案通知怎么样免费给网站做优化
  • Differentially Private Synthetic Text Generation for RAG——论文阅读
  • SQL入门:流程控制函数全解析
  • php网站虚拟机价格电子商务网站建设的意义
  • 【AES加密专题】6.功能函数的编写(3)
  • Windows安装Apache Kafka保姆级教程(图文详解+可视化管理工具)
  • java集合类的底层类是哪个
  • Arbess从入门到实战(13) - 使用Arbess+GitLab+Gradle实现Java项目自动化部署
  • 类与对象--2
  • socket 传输结构体数据,另一端进行恢复
  • 2025年数据治理平台解决方案:让数据真正可用、可信、可运营
  • 网站建设公司 长春广州购物网站建设
  • Linux ARM QT FrameBuffer
  • 静态网站特点阿里云wordpress在哪里设置
  • MATLAB完整问卷调查数据分析(附完整代码)
  • Claude Code学习笔记(四)-助你快速搭建首个Python项目
  • 济南 制作网站 公司哪家好文登seo排名