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 来在应用上下文刷新之前对其进行自定义配置。

相关推荐
Gary Studio1 小时前
rk芯片驱动编写
linux·学习
mango_mangojuice1 小时前
Linux学习笔记(make/Makefile)1.23
java·linux·前端·笔记·学习
lingggggaaaa2 小时前
安全工具篇&动态绕过&DumpLsass凭据&Certutil下载&变异替换&打乱源头特征
学习·安全·web安全·免杀对抗
PP东2 小时前
Flowable学习(二)——Flowable概念学习
java·后端·学习·flowable
学电子她就能回来吗2 小时前
深度学习速成:损失函数与反向传播
人工智能·深度学习·学习·计算机视觉·github
AI视觉网奇4 小时前
ue 角色驱动衣服 绑定衣服
笔记·学习·ue5
张3蜂4 小时前
深入理解 Python 的 frozenset:为什么要有“不可变集合”?
前端·python·spring
Coder_Boy_5 小时前
基于Spring AI的分布式在线考试系统-事件处理架构实现方案
人工智能·spring boot·分布式·spring
wdfk_prog5 小时前
[Linux]学习笔记系列 -- [drivers][input]serio
linux·笔记·学习
7哥♡ۣۖᝰꫛꫀꪝۣℋ6 小时前
Spring-cloud\Eureka
java·spring·微服务·eureka