spring mvc源码学习笔记之九

news/2025/1/12 6:26:42/

在前面的文章中,我们简单讲了可以用 WebApplicationInitializer 接口去替换 web.xml
本文对这一块再做个详细讲解。
WebApplicationInitializer 这个接口的 javadoc 中有提到可以用继承 AbstractAnnotationConfigDispatcherServletInitializer 的方式替换实现 WebApplicationInitializer 接口。
先看代码,然后再具体解释。

  • pom.xml 的内容如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.qsm</groupId><artifactId>learn</artifactId><version>1.0.0</version></parent><groupId>com.qs</groupId><artifactId>demo-47</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.28</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version><scope>provided</scope></dependency></dependencies>
</project>
  • com.qs.demo.MyWebApplicationInitializer 的内容如下
package com.qs.demo;import com.qs.demo.config.RootApplicationContextConfig;
import com.qs.demo.config.WebApplicationContextConfig;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;/*** @author qs* @date 2023/07/20*/
public class MyWebApplicationInitializerextends AbstractAnnotationConfigDispatcherServletInitializer {@Overrideprotected Class<?>[] getRootConfigClasses() {return new Class[] {RootApplicationContextConfig.class};}@Overrideprotected Class<?>[] getServletConfigClasses() {return new Class[] {WebApplicationContextConfig.class};}@Overrideprotected String[] getServletMappings() {return new String[] {"/"};}
}
  • com.qs.demo.config.RootApplicationContextConfig 的内容如下
package com.qs.demo.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;/*** @author qs* @date 2023/07/20*/
@Configuration
@ComponentScan(basePackages = {"com.qs.demo.service", "com.qs.demo.dao"})
public class RootApplicationContextConfig {}
  • com.qs.demo.config.WebApplicationContextConfig 的内容如下
package com.qs.demo.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;/*** @author qs* @date 2023/07/20*/
@Configuration
@ComponentScan(basePackages = {"com.qs.demo.controller"})
public class WebApplicationContextConfig {}
  • com.qs.demo.controller.PeopleController 的内容如下
package com.qs.demo.controller;import com.qs.demo.service.PeopleService;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author qs* @date 2023/07/20*/
@RequestMapping("/people")
@RestController
public class PeopleController {private final PeopleService peopleService;public PeopleController(PeopleService peopleService) {this.peopleService = peopleService;}@GetMapping("/01")public String a() {return peopleService.a();}
}
  • com.qs.demo.service.PeopleService 的内容如下
package com.qs.demo.service;import com.qs.demo.dao.PeopleDao;import org.springframework.stereotype.Service;/*** @author qs* @date 2023/07/20*/
@Service
public class PeopleService {private final PeopleDao peopleDao;public PeopleService(PeopleDao peopleDao) {this.peopleDao = peopleDao;}public String a() {return peopleDao.a();}
}
  • com.qs.demo.dao.PeopleDao 的内容如下
package com.qs.demo.dao;import org.springframework.stereotype.Repository;import java.util.UUID;/*** @author qs* @date 2023/07/20*/
@Repository
public class PeopleDao {public String a() {return UUID.randomUUID().toString();}}

以上就是全部代码

接下来重点分析下 AbstractAnnotationConfigDispatcherServletInitializer 这个类,源代码不多,如下

/** Copyright 2002-2018 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      https://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.web.servlet.support;import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;/*** 下面这句话意思是:这个类是一个 WebApplicationInitializer(这个抽象类实现了 WebApplicationInitializer 接口)。* 它用于注册 DispatcherServlet 。* 并且,它使用 Java-based 的 spring 配置,也就是基于注解的配置方式。** {@link org.springframework.web.WebApplicationInitializer WebApplicationInitializer}* to register a {@code DispatcherServlet} and use Java-based Spring configuration.** 这个类的实现者需要重写 getRootConfigClasses 这个抽象方法。用于指定 root 应用上下文的配置类。* 并且这个括号里边也备注了,所谓的 root 应用上下文,应该是非 web 相关的。啥意思呢,粗暴理解,就是 service | dao 这些东西放到 root 应用上下文。** 这个类的实现者需要重写 getServletConfigClasses 这个抽象方法。用于指定 DispatcherServlet 的应用上下文的配置类。* 在这个配置类中配置的是跟 spring mvc 相关的配置信息。** 对比这2个方法。我们可以看出。父子容器设定的一个出发点就是将web相关的东西和非web相关的东西隔离开。** <p>Implementations are required to implement:* <ul>* <li>{@link #getRootConfigClasses()} -- for "root" application context (non-web* infrastructure) configuration.* <li>{@link #getServletConfigClasses()} -- for {@code DispatcherServlet}* application context (Spring MVC infrastructure) configuration.* </ul>** 下面这段话意思是,如果不需要父子容器的话,可以将所有配置写到 getRootConfigClasses 指定的配置类中,* 让 getServletConfigClasses 返回 null。** <p>If an application context hierarchy is not required, applications may* return all configuration via {@link #getRootConfigClasses()} and return* {@code null} from {@link #getServletConfigClasses()}.** @author Arjen Poutsma* @author Chris Beams* @since 3.2*/
public abstract class AbstractAnnotationConfigDispatcherServletInitializerextends AbstractDispatcherServletInitializer {/*** {@inheritDoc}* <p>This implementation creates an {@link AnnotationConfigWebApplicationContext},* providing it the annotated classes returned by {@link #getRootConfigClasses()}.* Returns {@code null} if {@link #getRootConfigClasses()} returns {@code null}.*/@Override@Nullableprotected WebApplicationContext createRootApplicationContext() {System.out.println("所谓创建根web应用上下文就是 new AnnotationConfigWebApplicationContext() ");Class<?>[] configClasses = getRootConfigClasses();if (!ObjectUtils.isEmpty(configClasses)) {AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();context.register(configClasses);return context;}else {return null;}}/*** {@inheritDoc}* <p>This implementation creates an {@link AnnotationConfigWebApplicationContext},* providing it the annotated classes returned by {@link #getServletConfigClasses()}.*/@Overrideprotected WebApplicationContext createServletApplicationContext() {System.out.println("createServletApplicationContext ----> new AnnotationConfigWebApplicationContext() ");AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();Class<?>[] configClasses = getServletConfigClasses();if (!ObjectUtils.isEmpty(configClasses)) {context.register(configClasses);}return context;}/*** Specify {@code @Configuration} and/or {@code @Component} classes for the* {@linkplain #createRootApplicationContext() root application context}.* @return the configuration for the root application context, or {@code null}* if creation and registration of a root context is not desired*/@Nullableprotected abstract Class<?>[] getRootConfigClasses();/*** Specify {@code @Configuration} and/or {@code @Component} classes for the* {@linkplain #createServletApplicationContext() Servlet application context}.* @return the configuration for the Servlet application context, or* {@code null} if all configuration is specified through root config classes.*/@Nullableprotected abstract Class<?>[] getServletConfigClasses();}

最后,AbstractAnnotationConfigDispatcherServletInitializer 这个抽象类的父类 AbstractDispatcherServletInitializer以及祖父 AbstractContextLoaderInitializer 都值得看看。


http://www.ppmy.cn/news/1562424.html

相关文章

LeetCode 2765. 最长交替子数组解析与解题思路

LeetCode 2765. 最长交替子数组解析与解题思路 在本篇博客中&#xff0c;我们将深入解析 LeetCode 上的一道有趣题目 —— 2765. 最长交替子数组。我们将通过题目理解、解题思路、代码实现以及示例解析&#xff0c;全面掌握这道题目的解决方法。 题目描述 给定一个下标从 0 …

网站运营数据pv、uv、ip

想要彻底弄清楚pv uv ip的区别&#xff0c;首先要知道三者的定义&#xff1a; IP(独立IP)的定义&#xff1a; 即Internet Protocol,指独立IP数。24小时内相同公网IP地址只被计算一次。 PV(访问量)的定义&#xff1a; 即Page View,即页面浏览量或点击量&#xff0c;用户每次刷…

MySQL 如何实现可重复读?

文章目录 MySQL 可重复读的实现原理1. 多版本并发控制&#xff08;MVCC&#xff09;2. 隐藏列3. 读视图&#xff08;ReadView&#xff09;4. 版本链 案例说明案例 1&#xff1a;避免不可重复读案例 2&#xff1a;避免幻读 说明 MySQL 可重复读的实现原理 1. 多版本并发控制&am…

【PPTist】查找替换、绘制文本框

一、查找、替换 查找替换的组件 src/views/Editor/SearchPanel.vue 回车的时候会执行查找&#xff0c;查找替换功能的相关方法和属性都在 src/hooks/useSearch.ts 中。 1、查找 查找的方法也比较的朴实无华&#xff0c;就是 for 循环所有的幻灯片中的所有元素&#xff0c;通…

【20250109】Nature子刊:一种可改善帕金森患者冻结步态的柔性可穿戴机器人系统...

引言&#xff1a;冻结步态&#xff08;FoG&#xff09;是帕金森病中一种极具破坏性的步态障碍&#xff0c;会导致患者在行走时意外停顿。目前针对冻结步态的治疗效果有限且短暂&#xff0c;因此缺乏有效的治疗方法。该论文展示了一种使用机器人改善帕金森冻结步态的概念验证&am…

自然语言处理之jieba分词和TF-IDF分析

jieba分词和TF-IDF分析 目录 jieba分词和TF-IDF分析1 jieba1.1 简介1.2 终端下载1.3 基本语法 2 TF-IDF分析2.1 什么是语料库2.2 TF2.3 IDF2.4 TF-IDF2.5 函数导入2.6 方法 3 实际测试3.1 问题解析3.2 代码测试 1 jieba 1.1 简介 结巴分词&#xff08;Jieba&#xff09;是一个…

Web后端开发总结(day14)

Web后端开发总结 web后端开发现在基本上都是基于标准的三层架构进行开发的&#xff0c;在三层架构当中&#xff0c;Controller控制器 层负责接收请求响应数据&#xff0c;Service业务层负责具体的业务逻辑处理&#xff0c;Dao数据访问层也叫持久层&#xff0c; 就是用来处理数据…

MyBatis深入了解

目录 xml 映射文件中&#xff0c;除了常见的select、insert、update、delete 标签之外&#xff0c;还有哪些标签? Dao 接口的工作原理是什么?Dao 接口里的方法&#xff0c;参数不同时&#xff0c;方法能重载吗? MyBatis 是如何进行分页的?分页插件的原理是什么? 简述 …