首先我们来看一下web.xml,以及里面的配置具体是干什么的
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 id="WebApp_ID" version="2.5"> 6 7 <display-name>Archetype Created Web Application</display-name> 8 9 <filter> 10 <filter-name>characterEncodingFilter</filter-name> 11 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 12 <init-param> 13 <param-name>encoding</param-name> 14 <param-value>UTF-8</param-value> 15 </init-param> 16 <init-param> 17 <param-name>forceEncoding</param-name> 18 <param-value>true</param-value> 19 </init-param> 20 </filter> 21 <filter-mapping> 22 <filter-name>characterEncodingFilter</filter-name> 23 <url-pattern>/*</url-pattern> 24 </filter-mapping> 25 26 27 <listener> 28 <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 29 </listener> 30 31 <listener> 32 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 33 </listener> 34 <context-param> 35 <param-name>contextConfigLocation</param-name> 36 <param-value> 37 classpath:applicationContext.xml 38 </param-value> 39 </context-param> 40 41 <servlet> 42 <servlet-name>dispatcher</servlet-name> 43 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 44 <load-on-startup>1</load-on-startup> 45 </servlet> 46 47 48 49 <servlet-mapping> 50 <servlet-name>dispatcher</servlet-name> 51 <url-pattern>*.do</url-pattern> 52 </servlet-mapping> 53 54 </web-app>
第一个是一个过滤器的配置,这个配置是为了转码用的,用的是org.springframework.web.filter.CharacterEncodingFilter,拦截路径是/*,也就是所有的请求都会拦截,通过spring自己的类转化成utf-8
1 <filter> 2 <filter-name>characterEncodingFilter</filter-name> 3 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 4 <init-param> 5 <param-name>encoding</param-name> 6 <param-value>UTF-8</param-value> 7 </init-param> 8 <init-param> 9 <param-name>forceEncoding</param-name> 10 <param-value>true</param-value> 11 </init-param> 12 </filter> 13 <filter-mapping> 14 <filter-name>characterEncodingFilter</filter-name> 15 <url-pattern>/*</url-pattern> 16 </filter-mapping>
这是一个监听器,监听web容器启动和关闭
1 <listener> 2 <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 3 </listener>
这是一个为了将web容器和spring容器进行整合的一个监听器,下边的context-param,param-value指向的是我们的spring配置文件,ContextLoaderListener就会通过这个配置文件将web容器和spring容器进行整合
1 <listener> 2 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 3 </listener> 4 <context-param> 5 <param-name>contextConfigLocation</param-name> 6 <param-value> 7 classpath:applicationContext.xml 8 </param-value> 9 </context-param>
这个servlet就是配置springMVC的一个配置,servlet-name:dispatcher对应的是下边的servlet-mapping,*.do的请求都会被springMVC进行拦截,load-on-startup,这是一个servlet的配置,当它为0或大于0的时候,我们的容器在启动时就会初始化这个servlet,也就是dispatcher,当它小于0或不指定时,只有当这个servlet被选择,被使用时才加载
1 <servlet> 2 <servlet-name>dispatcher</servlet-name> 3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 4 <load-on-startup>1</load-on-startup> 5 </servlet> 6 7 8 9 <servlet-mapping> 10 <servlet-name>dispatcher</servlet-name> 11 <url-pattern>*.do</url-pattern> 12 </servlet-mapping>
接下来我们来看一下spring容器:applicationContext.xml的主配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 11 12 <context:component-scan base-package="com.mmall" annotation-config="true"/> 13 14 <!--<context:annotation-config/>--> 15 <aop:aspectj-autoproxy/> 16 17 18 <import resource="applicationContext-datasource.xml"/> 19 20 21 </beans>
12行是进行扫描,会扫描com.mmall下的一些注解,这样我们就可以很方便的在类中进行一些注入
18行是对spring配置文件进行的分离
那我们去看一下这个applicationContext-datasource.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 11 12 <context:component-scan base-package="com.mmall" annotation-config="true"/> 13 14 <bean id="propertyConfigurer" 15 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 16 <property name="order" value="2"/> 17 <property name="ignoreUnresolvablePlaceholders" value="true"/> 18 <property name="locations"> 19 <list> 20 <value>classpath:datasource.properties</value> 21 </list> 22 </property> 23 <property name="fileEncoding" value="utf-8"/> 24 </bean> 25 26 27 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 28 <property name="driverClassName" value="${db.driverClassName}"/> 29 <property name="url" value="${db.url}"/> 30 <property name="username" value="${db.username}"/> 31 <property name="password" value="${db.password}"/> 32 <!-- 连接池启动时的初始值 --> 33 <property name="initialSize" value="${db.initialSize}"/> 34 <!-- 连接池的最大值 --> 35 <property name="maxActive" value="${db.maxActive}"/> 36 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> 37 <property name="maxIdle" value="${db.maxIdle}"/> 38 <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> 39 <property name="minIdle" value="${db.minIdle}"/> 40 <!-- 最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制 --> 41 <property name="maxWait" value="${db.maxWait}"/> 42 <!--#给出一条简单的sql语句进行验证 --> 43 <!--<property name="validationQuery" value="select getdate()" />--> 44 <property name="defaultAutoCommit" value="${db.defaultAutoCommit}"/> 45 <!-- 回收被遗弃的(一般是忘了释放的)数据库连接到连接池中 --> 46 <!--<property name="removeAbandoned" value="true" />--> 47 <!-- 数据库连接过多长时间不用将被视为被遗弃而收回连接池中 --> 48 <!--<property name="removeAbandonedTimeout" value="120" />--> 49 <!-- #连接的超时时间,默认为半小时。 --> 50 <property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}"/> 51 52 <!--# 失效检查线程运行时间间隔,要小于MySQL默认--> 53 <property name="timeBetweenEvictionRunsMillis" value="40000"/> 54 <!--# 检查连接是否有效--> 55 <property name="testWhileIdle" value="true"/> 56 <!--# 检查连接有效性的SQL语句--> 57 <property name="validationQuery" value="SELECT 1 FROM dual"/> 58 </bean> 59 60 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 61 <property name="dataSource" ref="dataSource"/> 62 <property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"></property> 63 64 <!-- 分页插件 --> 65 <property name="plugins"> 66 <array> 67 <bean class="com.github.pagehelper.PageHelper"> 68 <property name="properties"> 69 <value> 70 dialect=mysql 71 </value> 72 </property> 73 </bean> 74 </array> 75 </property> 76 77 </bean> 78 79 <bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 80 <property name="basePackage" value="com.mmall.dao"/> 81 </bean> 82 83 <!-- 使用@Transactional进行声明式事务管理需要声明下面这行 --> 84 <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> 85 <!-- 事务管理 --> 86 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 87 <property name="dataSource" ref="dataSource"/> 88 <property name="rollbackOnCommitFailure" value="true"/> 89 </bean> 90 91 92 </beans>
propertyConfigurer这个bean,配置spring文件的时候,我们把里面的常量进行一个分离,分离到datasource.properties文件里面
1 <bean id="propertyConfigurer" 2 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 3 <property name="order" value="2"/> 4 <property name="ignoreUnresolvablePlaceholders" value="true"/> 5 <property name="locations"> 6 <list> 7 <value>classpath:datasource.properties</value> 8 </list> 9 </property> 10 <property name="fileEncoding" value="utf-8"/> 11 </bean>
至于datasource就不用多废话了,他通过上边这个propertyConfigurer bean来得到那些value
至于下边的sqlSessionFactory就比较重要了,看它的两个property,第一个,通过spring,把dataSource注入到对象,第二个,通过classpath,指定到我们的sql实现,分页插件就是分页插件咯
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"></property> <!-- 分页插件 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=mysql </value> </property> </bean> </array> </property> </bean>
这个是mybatis的一个扫描,它会扫描我们的dao层
1 <bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 2 <property name="basePackage" value="com.mmall.dao"/> 3 </bean>
事务管理了代码里有注释
接下来看下springMVC的配置:dispatcher-servlet.xml;可以在init-param中修改文件名,我们这里用的是初始化名字
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 8 http://www.springframework.org/schema/mvc 9 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 10 11 <context:component-scan base-package="com.mmall" annotation-config="true"/> 12 13 <mvc:annotation-driven> 14 <mvc:message-converters> 15 <bean class="org.springframework.http.converter.StringHttpMessageConverter"> 16 <property name="supportedMediaTypes"> 17 <list> 18 <value>text/plain;charset=UTF-8</value> 19 <value>text/html;charset=UTF-8</value> 20 </list> 21 </property> 22 </bean> 23 <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 24 <property name="supportedMediaTypes"> 25 <list> 26 <value>application/json;charset=UTF-8</value> 27 </list> 28 </property> 29 </bean> 30 </mvc:message-converters> 31 </mvc:annotation-driven> 32 33 34 35 <!-- 文件上传 --> 36 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 37 <property name="maxUploadSize" value="10485760"/> <!-- 10m --> 38 <property name="maxInMemorySize" value="4096" /> 39 <property name="defaultEncoding" value="UTF-8"></property> 40 </bean> 41 42 43 </beans>
这个配置是springMVC自动反序列化的一个配置
原文地址:https://www.cnblogs.com/zhangliang1726/p/9945868.html