Spring MVC 三 :基于注解配置

Servlet3.0

Servlet3.0是基于注解配置的理论基础。

Servlet3.0引入了基于注解配置Servlet的规范,提出了可拔插的ServletContext初始化方式,引入了一个叫ServletContainerInitializer的接口。

An instance of the ServletContainerInitializer is looked up via the jar services API by the container at container / application startup time. The framework providing an implementation of the ServletContainerInitializer MUST bundle in the META-INF/services directory of the jar file a file called javax.servlet.ServletContainerInitializer, as per the jar services API, that points to the implementation class of the ServletContainerInitializer.

Servlet3.0规范约定:WEB容器(比如tomcat)要通过SPI的方式检查应用jar包的META-INF/services目录下的Servlet容器的初始化类(ServletContainerInitializer接口的实现类),通过调用该实现类的onStartup方法完成Servlet容器的初始化。

此外,Servlet3.0还引入了@HandlesTypes注解,用来指定Servlet容器初始化过程中,WEB容器会认为应用中的哪些类(由@HandlesTypes指定)会参与到Servlet容器的初始化过程中来。

SpringMVC正是通过以上方式实现Servlet容器的初始化的!!!

SpringMVC Servlet容器初始化过程

按照上述理论的指引,探究注解方式配置Spring MVC的原理就没那么困难了。

先看下图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rcwAxqtb-1693146405131)(/img/bVc9kPt)]
很容易的,我们发现SpringMVC指定的Servlet容器初始化的实现类为org.springframework.web.SpringServletContainerInitializer。

所以我们找到他看看:

@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
}

确实是ServletContainerInitializer接口的实现类,@HandlesTypes指定的是WebApplicationInitializer,这个WebApplicationInitializer究竟是个啥东东,我们先放放,我们先来研究一下 SpringServletContainerInitializer类的onStartup方法。

@Overridepublic void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)throws ServletException {List<WebApplicationInitializer> initializers = new LinkedList<>();if (webAppInitializerClasses != null) {for (Class<?> waiClass : webAppInitializerClasses) {// Be defensive: Some servlet containers provide us with invalid classes,// no matter what @HandlesTypes says...if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) &&WebApplicationInitializer.class.isAssignableFrom(waiClass)) {try {initializers.add((WebApplicationInitializer)ReflectionUtils.accessibleConstructor(waiClass).newInstance());}catch (Throwable ex) {throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);}}}}if (initializers.isEmpty()) {servletContext.log("No Spring WebApplicationInitializer types detected on classpath");return;}servletContext.log(initializers.size() + " Spring WebApplicationInitializers detected on classpath");AnnotationAwareOrderComparator.sort(initializers);for (WebApplicationInitializer initializer : initializers) {initializer.onStartup(servletContext);}}}

代码逻辑也不算复杂,检查传入的参数webAppInitializerClasses,如果是实现类的话(不是接口或抽象类)则通过反射机制实例化webAppInitializerClasses并强转为WebApplicationInitializer,然后调用WebApplicationInitializer的onStartup方法。

这里有一个小问题:参数webAppInitializerClasses实例化之后,为什么能强转为WebApplicationInitializer?

其实这也是Servlet3.0规范约定的,WEB容器会根据@HandlesTypes的设置,从当前类加载器中查找符合条件的类,当前@HandlesTypes指定的正是WebApplicationInitializer。

之后的操作就都交给WebApplicationInitializer了。

WebApplicationInitializer

WebApplicationInitializer是承担起Servlet3.0规范约定的初始化Servlet容器的那个人:

Interface to be implemented in Servlet 3.0+ environments in order to configure the ServletContext programmatically – as opposed to (or possibly in conjunction with) the traditional web.xml-based approach.
Implementations of this SPI will be detected automatically by SpringServletContainerInitializer, which itself is bootstrapped automatically by any Servlet 3.0 container. See its Javadoc for details on this bootstrapping mechanism.

我们先看一下他的类结构:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ONu9uceL-1693146405132)(/img/bVc9kPW)]
Spring框架实现了WebApplicationInitializer接口的3个抽象类,最后一个抽象类AbstractAnnotationConfigDispatcherServletInitializer没有onStartup方法,onStartup方法是他的父类实现的。

我们看一下他的父类AbstractDispatcherServletInitializer的onStartup方法:

	@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {super.onStartup(servletContext);registerDispatcherServlet(servletContext);}

可以发现他其实是一个模板方法,首先调用了父类的onStartup,之后调用registerDispatcherServlet方法。父类的onStartup方法是实现rootApplicationContext调用的,至于什么是rootApplicationContext我们暂时不管,我们先看一下registerDispatcherServlet方法:

protected void registerDispatcherServlet(ServletContext servletContext) {String servletName = getServletName();Assert.hasLength(servletName, "getServletName() must not return null or empty");WebApplicationContext servletAppContext = createServletApplicationContext();Assert.notNull(servletAppContext, "createServletApplicationContext() must not return null");FrameworkServlet dispatcherServlet = createDispatcherServlet(servletAppContext);Assert.notNull(dispatcherServlet, "createDispatcherServlet(WebApplicationContext) must not return null");dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());...省略代码

首先调用createServletApplicationContext创建ServletApplicationContext(Servlet容器),之后创建DispathcerServlet并且把创建好的Servlet容器传递给DispatcherServlet(DispatcherServlet要记录他所在的ServletApplicationContext)。

关键代码出现了,Servlet容器的创建过程应该就在这个createServletApplicationContext方法中,是在AbstractAnnotationConfigDispatcherServletInitializer中实现的:

	@Overrideprotected WebApplicationContext createServletApplicationContext() {AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();Class<?>[] configClasses = getServletConfigClasses();if (!ObjectUtils.isEmpty(configClasses)) {context.register(configClasses);}return context;}

创建了一个新的AnnotationConfigWebApplicationContext对象,然后调用getServletConfigClasses()方法获取配置类,之后把配置类注册到了新创建的AnnotationConfigWebApplicationContext对象中。这个getServletConfigClasses()方法是没有实现的(应该是我们的实现类需要实现的)。

至此,Spring通过Servlet3.0规范进行初始化的过程应该已经很清晰了:
1.Spring Framework通过WebApplicationInitializer接口的onStartup方法完成Servlet上下文的初始化。
2.Spring Framework已经完成了WebApplicationInitializer接口的大部分实现(提供了3个抽象类),已经通过模板方法完成了大部分的初始化操作。
3. 猜测:应用层只需要扩展AbstractAnnotationConfigDispatcherServletInitializer类实现getServletConfigClasses()方法、返回Servlet的配置类,即可完成初始化。

接下来我们就验证一下上述猜测。

创建基于注解的Spring MVC应用

按照上述猜测,我们只需要扩展AbstractAnnotationConfigDispatcherServletInitializer、实现getServletConfigClasses()方法即可。

我们还是用上篇文章中用过的例子来验证,在动手之前,由于注解方式和web.xml配置方式是冲突的(配置方式会覆盖掉注解方式),所以我们需要删掉web.xml文件(copy出去即可)。

创建一个configuration包,并创建配置类(只要配置Controller的扫描路径即可):

package org.example.configuration;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration
@ComponentScan("org.example.controller")
public class MvcConfiguration {
}

然后再创建initializer的实现类:

package org.example.configuration;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;public class MvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {@Overrideprotected Class<?>[] getRootConfigClasses() {return new Class[0];}@Overrideprotected Class<?>[] getServletConfigClasses() {return new Class[] {MvcConfiguration.class};}@Overrideprotected String[] getServletMappings() {return new String[] {"/"};}
}

其中RootConfigClasse()方法是为RootApplicationContext服务的,我们前面说过了,展示不管这个RootApplicationContext是什么,现在依然也不管他。

getServletConfigClasses方法,返回我们创建的初始化类。

还有一个getServletMappings方法,上面没有提到过,其实是起到web.xml中的servlet-mapping配置的作用的,所以我们直接返回"/" - 默认匹配规则。

启动项目,访问:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PbumTEUE-1693146405132)(/img/bVc9kTd)]

大功告成!

上一篇 Spring MVC 二 :基于xml配置

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/55388.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

web层Controller通用处理(表格数据处理、分页数据、sql操作、客户端servlet、字符串工具类)

BaseController&#xff08;一&#xff09; public class BaseController{// slf4j-api-1.7.30.jarprotected final Logger logger LoggerFactory.getLogger(BaseController.class);/**将前端传递过来的日期格式字符串&#xff0c;转换为Date类型spring-web-5.3.1.jar*/InitB…

浅析阿里云灵积(平台)模型服务

简介&#xff1a; DashScope灵积模型服务以模型为中心&#xff0c;致力于面向AI应用开发者提供品类丰富、数量众多的模型选择&#xff0c;并为其提供开箱即用、能力卓越、成本经济的模型服务API。DashScope灵积模型服务依托达摩院等机构的优质模型&#xff0c;在阿里云基础设施…

软考高级系统架构设计师系列论文七十三:论中间件在SIM卡应用开发中的作用

软考高级系统架构设计师系列论文七十三:论中间件在SIM卡应用开发中的作用 一、中间件相关知识点二、摘要三、正文四、总结一、中间件相关知识点 软考高级系统架构设计师:构件与中间件技术二、摘要 我曾于近期参与过一个基于SIM卡应用的开发项目,并在项目中担任系统分析的工作…

Linux性能计数器

目录 一、简介二、sys_perf_event_open1、参数 struct perf_event_attr2、参数 pid3、参数 cpu4、参数 group_fd5、参数 flags6、记录信息的环形缓冲区 三、计数器(组)的启用或禁用四、示例代码 一、简介 性能计数器是在大多数现代cpu上可用的特殊硬件寄存器。这些寄存器统计特…

rke安装k8s

1、修改集群中各物理机主机名hostname文件 # 查看 cat /etc/hostname # 命令修改 hostnamectl set-hostname k8s-master2、实现主机名与ip地址解析 # 查看cat /etc/hosts # 修改 vi /etc/hosts3、配置ip_forward过滤机制 # 修改 vi /etc/sysctl.conf net.ipv4.ip_forward1…

CGY-OS 正式开源!【软件编写篇】

上一篇文章&#xff1a;CGY-OS 正式开源&#xff01;_cgy091107的博客-CSDN博客 一、软件编写基础要求 在编写CGY-OS的应用程序之前&#xff0c;您需要&#xff1a; 1. 安装python3.10&#xff0c;配置好CGY-OS。 2.掌握python3的基本语法、lambda表达式、各种简单的数据结构。…

【Python】PySpark

前言 Apache Spark是用于大规模数据&#xff08;large-scala data&#xff09;处理的统一&#xff08;unified&#xff09;分析引擎。 简单来说&#xff0c;Spark是一款分布式的计算框架&#xff0c;用于调度成百上千的服务器集群&#xff0c;计算TB、PB乃至EB级别的海量数据…

LabVIEW是如何控制硬件的?

概述 工程 师 和 科学 家 可以 使用 LabVIEW 与 数千 种 不同 的 硬件 设备 无缝 集成&#xff0c; 并 通过 方便 的 功能 和 跨 所有 硬件 的 一致 编 程 框架 帮助 节省 开发 时间。 内容 通过更简单的系统集成节省开发时间 连接到任何硬件 NI 硬件 第三方硬件 快速找到…

ubuntu18.04复现yolo v8之最终章,realsenseD435i+yolo v8完美运行

背景&#xff1a;上一篇博客我们已经为复现yolov8配置好了环境&#xff0c;如果前面的工作顺利进行&#xff0c;我们已经完成了90%&#xff08;学习类程序最难的是环境配置&#xff09;。 接下来将正式下载yolov8的相关代码&#xff0c;以及进行realsenseD435i相机yolo v8的de…

【学习FreeRTOS】第16章——FreeRTOS事件标志组

1.事件标志组简介 事件标志位&#xff1a;用一个位&#xff0c;来表示事件是否发生 事件标志组是一组事件标志位的集合&#xff0c; 可以简单的理解事件标志组&#xff0c;就是一个整数。 事件标志组的特点&#xff1a; 它的每一个位表示一个事件&#xff08;高8位不算&…

C++学习笔记总结练习:数组离散化的方法

数组离散化 1 问题描述 离散化一个序列的前提是我们只关心这个序列里面元素的相对大小&#xff0c;而不关心绝对大小&#xff08;即只关心元素在序列中的排名&#xff09;&#xff1b;离散化的目的是让原来分布零散的值聚集到一起&#xff0c;减少空间浪费。那么如何获得元素…

linux入门详解

文章目录 一、引言1.1 开发环境1.2 生产环境1.3 测试环境1.4 操作系统的选择 二、Linux介绍2.1 Linux介绍2.2 Linux的版本2.3 Linux和Windows区别 三、Linux安装3.1 安装VMware3.2 安装Xterm3.3 在VMware中安装Linux3.3.1 选择安装方式3.3.2 指定镜像方式3.3.3 选择操作系统类型…

qt创建临时文件

1、临时文件系统 在 Linux 系统中&#xff0c;创建临时文件系统很简单&#xff0c;执行如下指令即可&#xff1a; mount -t tmpfs -o size1024m tmpfs /mnt/tmp 挂载成功后&#xff0c;在 /mnt/tmp 这个挂载点下创建的所有文件都将会是临时文件, 也就是说&#xff1a;当电脑关…

《C语言编程环境搭建》工欲善其事 必先利其器

C语言编译器 GCC 系列 GNU编译器套装(英语&#xff1a;GNU Compiler Collection&#xff0c;缩写为GCC)&#xff0c;指一套编程语言编译器&#xff0c;常被认为是跨平台编译器的事实标准。原名是&#xff1a;GNU C语言编译器(GNU C Compiler)。 MinGW 又称mingw32 &#xff0c…

DevOps中的持续测试优势和工具

持续测试 DevOps中的持续测试是一种软件测试类型&#xff0c;它涉及在软件开发生命周期的每个阶段测试软件。持续测试的目标是通过早期测试和经常测试来评估持续交付过程的每一步的软件质量。 DevOps中的持续测试流程涉及开发人员、DevOps、QA和操作系统等利益相关者。 持续…

组件库的使用和自定义组件

目录 一、组件库介绍 1、什么是组件 2、组件库介绍 3、arco.design 二、组件库的使用 1、快速上手 2、主题定制 3、暗黑模式 4、语言国际化 5、业务常见问题 三、自定义组件 2、组件开发规范 3、示例实践guide-tip 4、业务组件快速托管 一、组件库介绍 1、什么是…

9个python自动化脚本,PPT批量生成缩略图、添加图片、重命名

引言 最近一番在整理资料&#xff0c;之前买的PPT资源很大很多&#xff0c;但归类并不好&#xff0c;于是一番准备把这些PPT资源重新整理一下。统计了下&#xff0c;这些PPT资源大概有2000多个&#xff0c;一共30多G&#xff0c;一个一个手动整理这个投入产出比也太低了。 作为…

openGauss学习笔记-49 openGauss 高级特性-索引推荐

文章目录 openGauss学习笔记-49 openGauss 高级特性-索引推荐49.1 单query索引推荐49.2 虚拟索引49.3 workload级别索引推荐 openGauss学习笔记-49 openGauss 高级特性-索引推荐 openGauss的索引推荐的功能&#xff0c;共包含三个子功能&#xff1a;单query索引推荐、虚拟索引…

数据结构基础:P3-树(上)----编程作业02:List Leaves

本系列文章为浙江大学陈越、何钦铭数据结构学习笔记&#xff0c;系列文章链接如下&#xff1a; 数据结构(陈越、何钦铭)学习笔记 文章目录 一、题目描述二、整体思路与实现代码 一、题目描述 题目描述&#xff1a; 给定一棵树&#xff0c;按照从上到下、从左到右的顺序列出所有…

【填坑向】MySQL常见报错及处理系列(ERROR! The server quit without updating PID file)

本系列其他文章 【填坑向】MySQL常见报错及处理系列&#xff08;Communications link failure & Access denied for user ‘root‘‘localhost‘&#xff09;_AQin1012的博客-CSDN博客翻一下大致的意思就是默认会按照如下的顺序读取配置文件&#xff0c;我上面贴出的配置文…