实训Day05

今日知识

  • Spring
  • Spring MVC

Spring

优点

  • Spring 是一个开源的免费的框架(容器)
  • Spring 是一个轻量级的、非入侵式的框架
  • 控制反转(IOC),面向切面编程(AOP)
  • 支持事务的处理,对框架整合的支持

组成

Spring 组成

拓展

  • SpringBoot
    • 一个快速开发的脚手架
    • 基于 SpringBoot 可以快速开发单个微服务
    • 约定大于配置
  • SpringCloud
    • SpringCloud 基于 SpringBoot 实现的

IOC(Inversion of Control)

如果程序代码量十分庞大,修改一次的成本代价将会十分昂贵!可以使用实现 set 接口进行动态注入,将对象的创建权限由程序交给程序员。系统的耦合性大大降低。

DI(Dependency Injection)

控制反转是一种设计思想,则依赖注入是实现IOC的一种方法。

采用 XML 方式配置 Bean 的时候,Bean 的定义信息是和实现分离的,而采用注解的方式可以把两者合为一体。Bean 的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的。

实现案例

配置依赖

1
2
3
4
5
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.3</version>
<dependency>

配置文件

1
2
3
4
5
6
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

创建容器

1
2
3
4
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("xxx.xml");
// 在配置文件加载时,容器中管理的对象就已经初始化了
}

Spring Bean

什么是Bean

Spring Bean是被实例的,组装的及被Spring 容器管理的Java对象

生命周期

Bean生命周期

Bean的配置

xml
1
2
3
4
<bean id="id" class="com.example.xxx" name="name1 name2, name3; name4">
<property name="name1" value="xxx"/>
<property name="name2" ref="xxx"/>
</bean>
注解

开启注解开发

1
<context:component-scan base-package="需要被扫描的路径"/>

要求:

  • 需要实现无参构造
  • 需要被 Spring 扫描到
1
2
3
4
5
6
@Component("id")
// 注解标记该类为 Bean 对象
public class DemoImpl implements Demo {

}

IOC创建对象的方式

  1. 默认无参构造
  2. 有参构造
    1. 下标赋值
    2. 类型
    3. 参数名

Spring配置

别名

1
<alias name="id" alias="name1"/>

Import

可导入其他开发人员的 xml 文件

1
<import resource="bean.xml">

依赖注入

  1. 构造注入
  2. Set注入
  3. 拓展注入
  4. Bean作用域
    • 单例
    • 多例

自动装配

自动装配是 Spring 满足 bean 依赖的一种方式
Spring 会在上下文中自动寻找,并自动给 bean 装配属性

  1. ByName
    1. 确保所有 bean 的 id 唯一
    2. bean 的 id 和需要自动注入的类中的 set 方法后面的名字一致
  2. ByType
    1. 确保所有 bean 的 class 唯一
    2. bean 的属性和需要自动注入的类中的属性的类型一致

注解开发

开启注解的支持

1
<context:annotation-config>

且需要导入 aop 包

  • @Component 组件
    • @ Dao
    • @ Service
    • @Controller
  • @Value 注入值
  • @Autowired 默认 ByType
  • @Nullable 可以为 null
  • @Resource 默认 ByName

AOP(Aspect Oriented Programming)

什么是AOP

面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP 是 OOP 的延续,是软件开发中的一个热点,也是 Spring 框架中的一个重要内容,是函数式编程的一种衍生范型。利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

AOP

术语

  • 横切关注点:跨越应用程序多个模块的方法或功能
  • 切面(Aspect):横切关注点被模块化的特殊对象
  • 通知(Advice):切面必须要完成的工作
  • 目标(Target):被通知对象
  • 代理(Proxy):向目标对象应用通知之后创建的对象
  • 切入点(PointCut):切面通知执行的”地点“的定义
  • 连接点(JoinPoint):与切入点匹配的执行点

通知类型

通知类型 连接点 实现接口
前置通知 方法前 MethodBeforeAdvice
后置通知 方法后 AfterReturningAdvice
环绕通知 方法前后 MethodInterceptor
异常抛出 方法抛出异常 ThrowsAdive
引介通知 类中增加新的方法属性 IntroductionInterceptor

xml文件配置

TODO

Spring MVC

什么是Spring MVC

是 Spring 内置的 MVC 框架

MVC(Model - View - Controller)

作用

MVC 框架,它解决了 Web 开发中的常见问题,而且使用简单,与 Spring 无缝集成。支持 RESTful 风格的 URL 请求。解决页面代码和后台代码的分离

原理

使用 Servlet 开发在接收请求参数,数据共享,页面跳转等操作相对比较复杂。而 Spring MVC 底层就是 Servlet,对其进行了更深层次封装。

SpringMVC 执行流程和原理

其中:

前端控制器由框架提供,接收请求,响应结果。
处理器映射器由框架提供,根据 url 查找 Handler
处理器适配器由框架提供,执行 Handler 的方法
处理器 Handler由程序员开发,接收用户请求信息,调用业务方法处理请求
视图解析器由框架提供,进行视图解析,把逻辑视图解析成真正的物理视图
视图由程序员开发,把数据展现给用户的界面

所以程序员只需要完成三个工作:

  1. 配置前端控制器(web.xml 或注解)
  2. 开发后端控制器 Handler/Controller
  3. 开发视图 View

配置

maven依赖

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.23</version>
<!-- 版本太高会导致不兼容 -->
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.23</version>
</dependency>

Spring配置

1
2
3
4
<!-- 配置自动装配 -->
<context:annotation-config/>
<!-- 配置RequestMapping注解功能 -->
<mvc:annotation-driven/>

web.xml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置前端控制器 -->

<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
<!-- 读取 application 文件启动 ioc 容器 -->
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
<!-- 配置拦截路径 url -->
</servlet-mapping>

前端控制器

在 MVC 框架中都存在一个前端控制器,在 WEB 应用的前端(Front)设置一个入口控制器(Controller),是用来提供一个集中的请求处理机制,所有的请求都被发往该控制器统一处理,然后把请求分发给各自相应的处理程序。一般用来做一个共同的处理,如权限检查,授权,日志记录等。因为前端控制的集中处理请求的能力,因此提高了可重用性和可拓展性。

代码风格

返回结果类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Result<T> {
private int code = 0;
// 状态码
private String msg = "";
// 消息
private List<T> data;
// 数据

public void setCode(int code) {
this.code = code;
}

public int getCode() {
return code;
}

public void setMsg(String msg) {
this.msg = msg;
}

public String getMsg() {
return msg;
}

public void setData(List<T> data) {
this.data = data;
}

public List<T> getData() {
return data;
}
}

返回结果状态码(类似宏定义)

1
2
3
4
public class ResultCode {
public static int OK = 200;
public static int FAILURE = 400;
}

结果类的工厂类

1
2
3
4
5
6
7
8
9
10
11
public class ResultFactory {
public static <T> Result<T> buildResult(int code, String msg, List<T> data) {
Result<T> result = new Result<>();
// 生成新对象
result.setCode(code);
result.setMsg(msg);
result.setData(data);
// 设置好对应的值并返回
return result;
}
}

拦截器

开发步骤

  • 实现 HandlerInterceptor
  • 实现未实现的方法
    • preHandle 调用控制器方法之前会执行该方法,true 代表放行,false 代表终止
    • postHandle 控制器方法之后,视图渲染之前
    • afterCompletion 无论控制器是否成功都会执行,视图渲染之后
  • 配置

pom.xml 文件配置

1
2
3
4
5
6
7
8
9
10
11
12
<bean id="xxx", class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<!-- 加入自定义的类型转换器 -->
<bean class="xxx.xxx.xxx">
</set>
</property>
</bean>

<mvc:interceptors>
<bean class="拦截器全限定名"/>
</mvc:interceptors>

实训Day05
http://yjh-2860674406.github.io/2023/07/09/编程/实训/Day05/
Author
Ye JinHua
Posted on
July 9, 2023
Licensed under