spring mvc源码学习笔记之二

  • pom.xml 内容如下
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>

    <groupId>com.qs.demo</groupId>
    <artifactId>test-010</artifactId>
    <version>1.0-SNAPSHOT</version>

    <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>4.3.30.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>
  • src/main/webapp/WEB-INF/web.xml 内容如下
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>app</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextInitializerClasses</param-name>
            <!-- 值可以用逗号、分号、换行分割 -->
            <param-value>com.qs.demo.A_ApplicationContextInitializer</param-value>
        </init-param>
        <!-- 小小优化,加快首次访问速度 -->
        <load-on-startup>0</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>app</servlet-name>
        <!-- / 表示除了 xxx.jsp 之外的所有请求 -->
        <!-- /* 表示所有请求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
  • com.qs.demo.A_ApplicationContextInitializer 内容如下
java 复制代码
package com.qs.demo;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

/**
 * @author qs
 * @date 2024/09/24
 */
public class A_ApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
  @Override
  public void initialize(ConfigurableApplicationContext applicationContext) {
    System.out.println(this.getClass().getName() + " 参考 ContextLoader ");
    ((AnnotationConfigWebApplicationContext) applicationContext).register(Conf.class);
  }
}
  • com.qs.demo.Conf 内容如下
java 复制代码
package com.qs.demo;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author qs
 * @date 2025/01/03
 */
@Configuration
@ComponentScan(basePackages = {"com.qs.demo"})
public class Conf {

}
  • com.qs.demo.FirstController 内容如下
java 复制代码
package com.qs.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author qs
 * @date 2024/12/20
 */
@Controller
public class FirstController {

  @RequestMapping("/t01")
  @ResponseBody
  public void t01() {
    System.out.println("---------> " + System.currentTimeMillis());
  }

  @RequestMapping("/t02")
  @ResponseBody
  public String t02() {
    return "---------> " + System.currentTimeMillis();
  }

}

以上就是全部代码

写这个例子主要是看名为 contextClass 的 servlet init-param 的用法。

在上面的例子中,我们用 contextClass 指定了使用 AnnotationConfigWebApplicationContext。

同时还使用 contextInitializerClasses 指定了用 com.qs.demo.A_ApplicationContextInitializer 来在应用上下文刷新之前对其进行自定义配置。

相关推荐
布局呆星3 小时前
SpringBoot 基础入门
java·spring boot·spring
炽烈小老头5 小时前
【 每天学习一点算法 2026/04/12】x 的平方根
学习·算法
阿杰学AI6 小时前
AI核心知识115—大语言模型之 自监督学习(简洁且通俗易懂版)
人工智能·学习·ai·语言模型·aigc·监督学习·自监督学习
九英里路7 小时前
OS学习之路——动静态库制作与原理
linux·学习·操作系统·unix·进程·编译·动静态库
❀͜͡傀儡师7 小时前
Spring AI Alibaba vs. AgentScope:两个阿里AI框架,如何选择?
java·人工智能·spring
red_redemption7 小时前
自由学习记录(160)
学习
南無忘码至尊7 小时前
Unity学习90天-第2天-认识Unity生命周期函数并用 Update 控制物体移动,FixedUpdate 控制物理
学习·unity·游戏引擎
报错小能手8 小时前
ios开发方向——swift错误处理:do/try/catch、Result、throws
开发语言·学习·ios·swift
LX567778 小时前
传统销售如何系统学习成为AI智能销售顾问?认证指南
人工智能·学习
做cv的小昊8 小时前
【TJU】应用统计学——第五周作业(3.1 假设检验的基本思想、3.2 单个正态总体参数的假设检验)
学习·线性代数·机器学习·数学建模·矩阵·概率论·tju