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>
    <parent>
        <groupId>com.qsm</groupId>
        <artifactId>learn</artifactId>
        <version>1.0.0</version>
    </parent>

    <groupId>com.qs</groupId>
    <artifactId>demo-40</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>
  • 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"
         metadata-complete="true">

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/app-context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>app</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/qs-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>app</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
  • src/main/webapp/WEB-INF/app-context.xml 的内容如下
xml 复制代码
<?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">
    <bean id="peopleService" class="com.qs.demo.root.PeopleService"/>
</beans>
  • src/main/webapp/WEB-INF/qs-servlet.xml 的内容如下
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.qs.demo.sub"/>
</beans>
  • com.qs.demo.root.PeopleService 的内容如下
java 复制代码
package com.qs.demo.root;
/**
 * @author qs
 * @date 2023/07/05
 */
public class PeopleService {

    public void a() {
        System.out.println("==== a");
    }

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

import com.qs.demo.root.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qs
 * @date 2023/07/05
 */
@RestController
public class PeopleController {

  @Autowired private ApplicationContext applicationContext;

  @Autowired
  @Qualifier("peopleService")
  private PeopleService peopleService;

  @GetMapping("/01")
  public String test01() {
    System.out.println("demo40 ======================= " + applicationContext.getClass().getName());
    return "demo40 01";
  }

  @GetMapping("/02")
  public String test02() {
    peopleService.a();
    return "demo40 02";
  }
}

以上就是全部代码

写这个例子主要是为了看父子容器这个事儿。

上面的例子可以分为2部分看,一部分是父容器相关的,一部分是子容器相关的。

先从 web.xml 来看

xml 复制代码
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/app-context.xml</param-value>
</context-param>

上面这段是配置父容器的。首先就是 ContextLoaderListener 这个类。它非常重要。准确说是它的父类 ContextLoader 非常重要。

因为 ContextLoaderListener 的很多事情都是在父类 ContextLoader 中完成的。

而名为 contextConfigLocation 的 context-param 是用来指定父容器要用的配置文件的。不指定的话,默认是用 /WEB-INF/applicationContext.xml 这个文件。

关于 ContextLoaderListener 和它的父类 ContextLoader 这2个类,我们会在其他文章中介绍。

接着看父容器的配置文件 /WEB-INF/app-context.xml。我们将 service 层的 bean 配置在父容器中。

xml 复制代码
<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">
    <bean id="peopleService" class="com.qs.demo.root.PeopleService"/>
</beans>

多说一嘴,spring 设计父子容器的一个目的就是想让我们把 service dao 层配置放到父容器,而跟视图层相关的配置也就是 spring mvc 中的控制器、视图解析器、处理器适配器、处理器映射器等放到子容器。扯远了,回来继续看子容器,也就是下面 web.xml 中下面这部分

xml 复制代码
<servlet>
    <servlet-name>app</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/qs-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

在子容器的配置文件 /WEB-INF/qs-servlet.xml 我们定义了组件扫描范围

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.qs.demo.sub"/>
</beans>

而我们的 controller 就在这个 com.qs.demo.sub 包下。

相关推荐
摇滚侠4 分钟前
MyBatis 入门到项目实战 MyBatis 各种查询功能 30-33
java·后端·spring·maven·intellij-idea·mybatis
老马识途2.010 小时前
在AI的帮助下理解spring的启动过程
java·前端·spring
不才不才不不才11 小时前
Spring AI 实战:聊天、提示词、记忆三件套
java·人工智能·spring·ai
袁小皮皮不皮12 小时前
1.HCIP BFD 学习笔记(优化版)
服务器·网络·笔记·网络协议·学习·智能路由器·ip
装不满的克莱因瓶12 小时前
【自动驾驶领域】学习 Cityscapes 数据集——城市街景语义理解的标准基准
人工智能·pytorch·python·深度学习·学习·机器学习·自动驾驶
清辞85313 小时前
产品经理需求推进流程
大数据·深度学习·学习·产品经理
YM52e13 小时前
鸿蒙PC ArkTS 声明合并问题深度解析与最佳实践
学习·华为·harmonyos·鸿蒙·鸿蒙系统
椰椰椰耶14 小时前
[SpringCloud][14]OpenFeign参数传递方法
后端·spring·spring cloud
海兰14 小时前
【实用程序】电商销售分析仪表盘 — 从零搭建一个AI参与的全栈数据洞察系统
人工智能·学习·算法
ken223215 小时前
在 Libreoffice Calc中输入自定义表情字符时,需要保存之后,才能正常显示
学习