背景
现在流行使用SpringBoot项目来开发Web项目,只要添加以下依赖即可拥有开箱即用的SpringMVC的开发环境。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
但是为了更好的理解Web项目和Spring MVC的原理,此文章使用原始的web.xml配置来展示如何配置一个Spring MVC项目。
配置
web.xml
Tomcat启动的时候,会读取该文件,并且初始化Servlet
<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!--通过容器监听器初始化Spring IOC容器 使用的配置文件路径为上面配置的路径-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--在DispatcherServlet的父类FrameworkServlet在执行init()方法的时候,
会初始化一个子Spring IOC容器,并将在监听器中初始化的父容器设置到parent属性中-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<!--在FrameworkServlet中初始化的子容器配置文件路径-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml
- 在ContextLoaderListener中初始化Spring容器的配置文件
- 主要用于配置业务相关的Bean,比如数据库相关,事务相关
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"
default-autowire="byName"
>
<!-- default-autowire="byName" 开启了自动装配 -->
<!--启用声明式事务-->
<tx:annotation-driven />
<!--当然是配置datasource了-->
<jdbc:embedded-database id="dataSource" type="H2">
<!--一定要是先DDL,即数据库定义语言-->
<jdbc:script location="classpath:sql/h2-schema.sql"/>
<!--然后才是DML,数据库操作语言-->
<jdbc:script location="classpath:sql/h2-data.sql" encoding="UTF-8"/>
</jdbc:embedded-database>
<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--查询服务-->
<bean id="userService" class="com.kunxian.springmvc.UserService">
</bean>
</beans>
dispatcher-servlet.xml
- DispatcherServlet的父类FrameworkServlet执行init()初始化Spring子容器的配置文件
- 主要用于配置mvc相关,比如Controller,视图解析器等
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<!--支持注解方式声明Controller-->
<mvc:annotation-driven />
<!-- 配置自动扫描Controller的包 -->
<context:component-scan base-package="com.kunxian.springmvc.controller"/>
<!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Controller
package com.kunxian.springmvc.controller;
import com.kunxian.springmvc.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.sql.SQLException;
@Controller
public class UserController {
@Autowired
UserService userService;
@GetMapping("/addUser.action")
@ResponseBody
public String addUser() throws SQLException {
userService.insertUser("小名",15);
return "addUser success";
}
}
Tomcat
配置一个已经安装的Tomcat用于部署Web项目
启动项目
自动弹出欢迎页
默认如果目录中存在index.jsp、index.html、index.htm,
启动成功后自动会弹出首页
访问Controller
总结
- 不同于SpringBoot项目的内嵌Tomcat,需要单独启动Tomcat
- Spring IOC容器初始化了两次
- 在ContextLoaderListener初始化了父容器
- 在DispatcherServlet初始化了子容器
思考
- Spring IOC容器为何要初始化两个容器
- SpringMVC的初始化过程,初始化了哪些组件
- DispatcherSerlvet的工作原理
- Tomcat接收到请求后,到底Controller的执行过程