tomcat在启动时,会加载web.xml文件,在加载web.xml文件时,会一起读取很多配置参数,如,,等等,servlet的加载有2种,一种是访问时才加载,一种是服务端启动时就加载(只需要设置为1即可)。
在web-app元素内,元素的配置顺序与工程的加载顺序无关,web.xml的加载过程为:
启动一个web项目,web容器(如tomcat)读取web.xml文件,读取其中的配置信息
容器创建一个servlet上下文(servletContext),这个web项目所有部分共享这个上下文
容器将转换为键值对,交给servletContext
容器创建中的监听器实例
触发contextInitialized方法,listener被调用(当Servlet 容器启动或终止Web 应用时,会触发ServletContextEvent 事件,该事件由ServletContextListener 来处理。ServletContextListener 接口中定义了处理ServletContextEvent 事件的两个方法contextInitialized;contextDestroyed,web.xml有contextLoaderListener监听器,spring等框架实现了本监听器的接口方法)
调用完contextInitialized方法后,容器再对filter初始化
SpringMVC组件包括:前端控制器,处理器映射器,处理器适配器,视图解析器,处理器Handler,视图View。其中,只有处理器Handler和视图View需要程序员开发。
Spring:实现对象的管理,用于管理Bean等,IOC容器 DI,AOP
Mybatis:负责与数据库进行交互设计,用来处理数据的持久化工作,简单理解就是Javaweb阶段的一些JDBC操作等等。
**SpringMVC框架核心其实就是一个Servlet,只是它是用一个Servlet来管理所有请求,因此,它的配置就是在web.xml中添加SpringMVC的Servlet分发器。**如下所示:
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- =====================扫描组件====================================--> <!--SpringMVC的配置文件,包含网站跳转逻辑的控制,配置 --> <context:component-scan base-package="com.nju" use-default-filters="false"> <!--只扫描控制器,也就是@Controller注解 --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--===================配置视图解析器(也可以使用其它的)===================== --> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!-- 视图前缀 --> <property name="prefix" value="/WEB-INF/templates/"/> <!-- 视图后缀 --> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8" /> </bean> </property> </bean> </property> </bean> <!-- 默认的视图解析器--> <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">--> <!-- <!– 配置逻辑视图的前缀 –>--> <!-- <property name="prefix" value="/WEB-INF/templates/" />--> <!-- <!– 配置逻辑视图的后缀 –>--> <!-- <property name="suffix" value=".html" />--> <!-- </bean>--> <!--===================配置视图解析器(也可以使用其它的)===================== --> <!-- 将springmvc不能处理的请求交给tomcat处理静态资源,例如html、js、css、jpg 在 WEB 容器启动的时候会在上下文中定义一个 DefaultServletHttpRequestHandler, 它会对DispatcherServlet的请求进行处理,如果该请求已经作了映射,那么会接着交给后台对应的处理程序, 如果没有作映射,就交给 WEB 应用服务器默认的 Servlet 处理,从而找到对应的静态资源,只有再找不到资源时才会报错。 原文链接:https://blog.csdn.net/codejas/article/details/80055608 --> <mvc:default-servlet-handler/> <!-- ========================开启mvc注解驱动================================= --> <mvc:annotation-driven> <mvc:message-converters> <!-- 处理响应中文内容乱码 --> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="defaultCharset" value="UTF-8" /> <property name="supportedMediaTypes"> <list> <value>text/html</value> <value>application/json</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!--视图控制器(可选)--> <mvc:view-controller path="/" view-name="index"></mvc:view-controller> </beans>
首先Spring需要自己的配置文件,一般都命名为:applicationContext.xml
。
由于Spring是SSM框架的核心,所以需要服务端启动时就进行加载运行,而能在服务端启动时就加载的文件一般可以利用的只有web.xml文件,所以spring加载设置还是放在了web.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--==========================扫描组件==========================--> <!--注意:这里区别与SpringMVC的配置文件,这里*不*扫描Controller控制器--> <context:component-scan base-package="com.nju"> <!--*不*扫描Controller控制器--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--======================Spring整合Mybatis======================--> <!--====1、配置连接池,此处使用druid===--> <!--加载文件--> <context:property-placeholder location="classpath:DruidDB.properties" /> <bean id="pooledDataSource" class="com.alibaba.druid.pool.DruidDataSource"> <!--数据库相关配置信息,也可以通过外部文件引入--> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="12345678"/> </bean> <!--===2、配置SqlSessionFactory,该工厂用于创建dao接口的代理对象,需要引用数据源-===--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定Mybatis配置文件所在位置--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!--指定数据源druid--> <property name="dataSource" ref="pooledDataSource"/> <!--指定*mapper.xml的位置--> <property name="mapperLocations" value="classpath*:/mapper/*.xml"/> </bean> <!--===3、配置dao接口对象所在位置,dao映射位置====--> <!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--扫描所有dao接口的实现,加入到ioc容器中 --> <property name="basePackage" value="com.nju.mapper"/> </bean> <!--====4、(根据实际需求配置)配置一个可以执行批量的sqlSession======--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/> <constructor-arg name="executorType" value="BATCH"/> </bean> <!--=====================配置spring框架声明式事务管理====================--> <!--1. 创建事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入数据源--> <property name="dataSource" ref="pooledDataSource"/> </bean> <!--2 配置通知,配置事务增强--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!--所有方法都是事务方法--> <tx:method name="*"/> <!--以get开始的所有方法--> <tx:method name="get" read-only="true"/> </tx:attributes> </tx:advice> <!--3 配置切入点和切面--> <aop:config> <!--配置切入点表达式--> <aop:pointcut id="txPoint" expression="execution(* com.nju.service..*(..))"/> <!--配置事务增强--> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/> </aop:config> </beans>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!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="com.nju.bean"/> </typeAliases> <plugins> <!--分页插件--> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!--分页参数合理化 --> <property name="reasonable" value="true"/> </plugin> </plugins> </configuration>
<?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 ================== --> <!-- 服务器启动是的所加载的参数,设置spring xml文件的位置 --> <!-- context-param标签将会被web容器优先读取转化为键值对交给servletContext,需要获取该键值对的值可以在servlet中 用getServletContext().getInitParameter获取。这里和servlet里的 init-param 类似,不过context-param是 所有的servlet共享的。 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- ====================配置监听器======================= --> <listener> <!--ContextLoaderListener能捕捉到服务器的启动和停止,在启动和停止触发里面的方法做相应的操作--> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- ====================启动SpringMVC==================== --> <!-- 其实做法和配置一个原生的servlet类似,只是使用SpringMVC只需要配置一个Servlet--> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 通过初始化参数指定SpringMVC配置文件的位置和名称 --> <init-param> <!-- contextConfigLocation为固定值 --> <param-name>contextConfigLocation</param-name> <!-- 使用classpath:表示从类路径查找配置文件,例如maven工程中的src/main/resources --> <param-value>classpath:SpringMVC.xml</param-value> </init-param> <!-- 作为框架的核心组件,在启动过程中有大量的初始化操作要做而这些操作放在第一次请求时才执行会严重影响访问速度 因此需要通过load-on-startup标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <!-- 设置springMVC的核心控制器所能处理的请求的请求路径 /所匹配的请求可以是/login或.html或.js或.css方式的请求路径 但是/不能匹配.jsp请求路径的请求 --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- ================= filter过滤器 ====================== --> <!-- 字符编码过滤器,一定要放在所有过滤器之前 --> <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> <init-param> <param-name>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- ================= Restful(可选) ====================== --> <!-- 4、使用Rest风格的URI,将页面普通的post请求转为指定的delete或者put请求 --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>HttpPutFormContentFilter</filter-name> <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class> </filter> <filter-mapping> <filter-name>HttpPutFormContentFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<?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.nju</groupId> <artifactId>ssm-crud</artifactId> <version>1.0-SNAPSHOT</version> <!--web工程--> <packaging>war</packaging> <!--引入项目依赖的jar包 --> <dependencies> <!--引入pageHelper分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.0</version> </dependency> <!-- MBG:实现Mybaits逆向工程 --> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> <!-- SpringMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.1</version> </dependency> <!-- 日志 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <!-- ServletAPI --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- Spring5和Thymeleaf整合包 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.12.RELEASE</version> </dependency> <!-- Spring-Jdbc --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!--Spring-test --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- Spring面向切面编程aspects --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!--MyBatis --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.2</version> </dependency> <!-- MyBatis整合Spring的适配包 --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- 数据库连接池、驱动 --> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!--mysql--> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- (jstl,servlet-api,junit) --> <!-- https://mvnrepository.com/artifact/jstl/jstl --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!-- junit --> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> <!--解决在测试的过程中出现: FAILED TO EXECUTE GOAL ORG.APACHE.MAVEN.PLUGINS:MAVEN-SUREFIRE-PLUGIN:2.12.4--> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4.2</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
https://blog.csdn.net/qq_17275841/article/details/88830334
https://www.freesion.com/article/4166575533/
https://blog.csdn.net/weixin_42176453/article/details/80263774