SpringMvc配置文件解析以及模板
SpringMvc是一种基于Java的实现MVC设计模型的请求驱动类型的轻量级Web框架。SpringMvc强大的注解开发功能使其全面超越Struts2,成为当下最流行的MVC框架。但是博主认为在学习一个全新的框架时候,我们应该先了解其基本的执行流程以及大概原理。注解开发确实为我们编写代码提供了便利,但是在一段时间后对于我们这些刚学习的小白是不利的,我们很快就会忘记相关集成组件原生代码环境、流程。
SpringMvc的纯配置文件方法可以让我们更好的理解整体的执行流程以及相关组件从原生环境到集成过程中的变化。我们以SSM整合为例介绍:
配置文件 | 功能 |
---|---|
mybatis核心配置文件(mybatis-configure.xml) | 核心配置文件配置 MyBatis 最核心的设置和属性信息,如数据库的连接、事务、连接池、别名设置、驼峰命名开启信息等 |
spring配置文件(spring.xml) | 将业务层中的实现类以及sqlSessionFactory工厂加载到spring的IOC容器中,交给容器管理 |
springmvc配置文件(springmvc.xml) | 初始DispatcherServlet前端控制器配置 |
web配置文件(web.xml) | 主要配置Listener、Servlet、Filter,简便web工程开发 |
1.项目结构图:
2.mybatis核心配置文件:
/**
约束头
*/
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 开启驼峰命名-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 配置包别名(直接不一一扫描每个实体类,直接扫描整个实体类包)-->
<typeAliases>
<package name="Tul.pojo"/>
</typeAliases>
</configuration>
我们将数据库连接配置和映射关系扫描放到spring.xml文件中进行处理。
核心文件常用配置(配置是有规定的顺序的):
configuration
properties(加载外部的properties文件)
settings
typeAliases(设置类型别名)
typeHandlers
objectFactory
plugins
environments(数据源环境的配置,支持多环境配置)
environment
transactionManager(事务控制)
dataSource
databaseIdProvider
mappers(加载映射配置)
spring配置文件:
/**
约束头
这里需要开一些新的命名空间,例如context、aop、tx等
*/
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 组件扫描 扫描service和mapper-->
<context:component-scan base-package="Tul">
<!-- 排除controller的扫描-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 加载properties文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!-- 使用druid的数据源(数据库连接配置)-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init">
<!-- 注入连接属性-->
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 设置初始化连接池大小-->
<property name="initialSize" value="5"></property>
<!-- 最大连接数-->
<property name="maxActive" value="10"></property>
<!-- 设置等待时间-->
<property name="maxWait" value="5000"></property>
<property name="filters" value="stat"></property>
</bean>
<!-- 配置sessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 加载mybatis核心文件-->
<property name="configLocation" value="classpath:mybatis/mybatis-configure.xml"></property>
<property name="mapperLocations" value="classpath:Tul.mapper/*.xml"/>
</bean>
<!-- 扫描mapper所在的包 为mapper创建实现类(这里采用的是动态代理的方法,加载映射关系)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="Tul.mapper"></property>
</bean>
<!-- 声明事务控制-->
<!-- 平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务增强-->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!--事务的aop织入-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* Tul.service.impl.*.*(..))"></aop:advisor>
</aop:config>
</beans>
properties文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=UTF-8&useSSL=false
jdbc.username=root
jdbc.password=142857
springmvc配置文件:
/**
约束头
这里一样需要开命名空间
*/
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 组件扫描 主要扫描controller-->
<context:component-scan base-package="Tul.controller"/>
<!-- 配置处理器映射器-->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->
<!-- 配置处理器适配器-->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->
<!-- 一个配置替换成上面的两个配置-->
<!-- <mvc:annotation-driven>会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean-->
<!-- 配置mvc注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 内部资源视图解析器-->
<bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view"></property>
<property name="suffix" value=".html"></property>
</bean>
<!-- 开发静态资源访问权限-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!-- 上面这种简便的静态资源放行方法 下面这种可能更直观一下-->
<!--<mvc:resources mapping="/static/**" location="/static/"/>-->
</beans>
web.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">
<!-- spring 监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring 的前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 初始化参数,指定springMVC配置文件的路径-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<!-- tomcat启动就要初始化servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<!-- 匹配除jsp外的一切资源-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- springmvc默认的是jsp-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!-- 乱码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这里讲一下springmvc的执行流程(因为目前csdn还不支持导入.drawio后缀的文件,这里截图说明,需要的小伙伴私信博主):
执行时序图:
执行原理图:
这个模板的controller层只写了一个方法getAll(),用来获取数据库user中的全部数据,运行显示截图:
项目运行环境是idea 2021.1.3版本+mysql 8.0.26+jdk 17 (经过测试 jdk 8也可以),源码在下面链接: