关于常用的web.xmlapplicationContext.xml与springMVC-servlet.xml

宁可枝头抱香死,何曾吹落北风中。这篇文章主要讲述关于常用的web.xmlapplicationContext.xml与springMVC-servlet.xml相关的知识,希望能为你提供帮助。
一、关于web.xml配置
 
这个没啥好说的,web启动需要加载的文件都扔这里。
 
 

< ?xml version="1.0" encoding="UTF-8"?> < web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> < welcome-file-list> < welcome-file> index.html< /welcome-file> < welcome-file> index.htm< /welcome-file> < welcome-file> index.jsp< /welcome-file> < welcome-file> default.html< /welcome-file> < welcome-file> default.htm< /welcome-file> < /welcome-file-list> < !-- Spring MVC配置 --> < !-- ====================================== --> < servlet> < servlet-name> springMVC< /servlet-name> < servlet-class> org.springframework.web.servlet.DispatcherServlet < /servlet-class> < !-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[< servlet-name> ]-servlet.xml,如springMVC-servlet.xml--> < init-param> < param-name> contextConfigLocation< /param-name> < param-value> /WEB-INF/springMVC-servlet.xml< /param-value> < /init-param> < load-on-startup> 1< /load-on-startup> < /servlet> < servlet-mapping> < servlet-name> springMVC< /servlet-name> < url-pattern> /< /url-pattern> < /servlet-mapping> < !-- Spring配置 --> < !-- ====================================== --> < listener> < listenerclass> org.springframework.web.context.ContextLoaderListener < /listener-class> < /listener> < !-- 指定Spring Bean以及数据源datasource的配置文件所在目录。默认配置在WEB-INF目录下 --> < context-param> < param-name> contextConfigLocation< /param-name> < param-value> classpath:config/applicationContext.xml< /param-value> < /context-param> < !-- ====================================== --> < !--加载其它属性文件--> < context-param> < param-name> config.file< /param-name> < param-value> /config.properties< /param-value> < /context-param> < !--配置字符编码过滤器--> < filter> < filter-name> encoding-filter< /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> forceEncoding< /param-name> < param-value> true< /param-value> < /init-param> < /filter> < filter-mapping> < filter-name> encoding-filter< /filter-name> < url-pattern> /*< /url-pattern> < /filter-mapping> < /web-app>

 
 
 
二、关于springMVC-servlet.xml配置
springMVC-servlet这个名字是因为上面web.xml中< servlet-name> 标签配的值为springMVC(< servlet-name> springMVC< /servlet-name> ),再加上“-servlet”后缀而形成的springMVC-servlet.xml文件名,如果改为spring,对应的文件名则为spring-servlet.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:p="http://www.springframework.org/schema/p" 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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> < !-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器--> < context:component-scan base-package="com" /> < !--mvc:message-converters消息转换器,它主要处理的是response返回的值,比如默认编码,比如支持Fastjson,当然你可以可以加入对其他返回类型的支持,比如gson,protobuf等等--> < mvc:annotation-driven> < mvc:message-converters register-defaults="true"> < !-- @ResponseBody乱码问题,将StringHttpMessageConverter的默认编码设为UTF-8 --> < bean class="org.springframework.http.converter.StringHttpMessageConverter"> < constructor-arg value="https://www.songbingjia.com/android/UTF-8" /> < /bean> < !-- 配置Fastjson支持 --> < bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> < property name="charset" value="https://www.songbingjia.com/android/UTF-8" /> < property name="supportedMediaTypes"> < list> < value> application/json< /value> < value> text/html; charset=UTF-8< /value> < /list> < /property> < property name="features"> < list> < value> WriteMapNullValue< /value> < value> QuoteFieldNames< /value> < value> WriteDateUseDateFormat< /value> < value> WriteEnumUsingToString< /value> < /list> < /property> < /bean> < /mvc:message-converters> < /mvc:annotation-driven> < !--对静态资源不拦截的处理--> < mvc:resources mapping="/static/**" location="classpath:/resources/static/**" /> < !--JSP视图解析器--> < bean id="viewResolverJsp" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> < property name="prefix" value="https://www.songbingjia.com/WEB-INF/jsp/"/> < property name="suffix" value="https://www.songbingjia.com/android/.jsp"/> < property name="viewClass" value="https://www.songbingjia.com/android/org.springframework.web.servlet.view.InternalResourceView"/> < property name="order" value="https://www.songbingjia.com/android/1"/> < /bean> < !-- 配置freeMarker视图解析器 --> < bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> < property name="viewClass" value="https://www.songbingjia.com/android/org.springframework.web.servlet.view.freemarker.FreeMarkerView"/> < property name="contentType" value="https://www.songbingjia.com/android/text/html; charset=UTF-8"/> < property name="exposeRequestAttributes" value="https://www.songbingjia.com/android/true" /> < property name="exposeSessionAttributes" value="https://www.songbingjia.com/android/true" /> < property name="exposeSpringMacroHelpers" value="https://www.songbingjia.com/android/true" /> < property name="requestContextAttribute" value="https://www.songbingjia.com/android/request" /> < property name="cache" value="https://www.songbingjia.com/android/true" /> < property name="suffix" value="https://www.songbingjia.com/android/.ftl" /> < property name="order" value="https://www.songbingjia.com/android/0"/> < /bean> < !-- 配置freeMarker的模板路径 --> < bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> < property name="templateLoaderPath" value="https://www.songbingjia.com/WEB-INF/freemarker/"/> < property name="freemarkerVariables"> < map> < entry key="xml_escape" value-ref="fmXmlEscape" /> < /map> < /property> < property name="defaultEncoding" value="https://www.songbingjia.com/android/UTF-8"/> < property name="freemarkerSettings"> < props> < prop key="template_update_delay"> 3600< /prop> < prop key="locale"> zh_CN< /prop> < prop key="datetime_format"> yyyy-MM-dd HH:mm:ss< /prop> < prop key="date_format"> yyyy-MM-dd< /prop> < prop key="number_format"> #.##< /prop> < /props> < /property> < /bean> < bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/> < !-- 配置文件上传解析器,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 --> < bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> < !-- 默认编码 --> < property name="defaultEncoding" value="https://www.songbingjia.com/android/utf-8" /> < !-- 文件大小最大值 --> < property name="maxUploadSize" value="https://www.songbingjia.com/android/10485760000" /> < !-- 内存中的最大值 --> < property name="maxInMemorySize" value="https://www.songbingjia.com/android/40960" /> < /bean> < !--统一处理异常的解析器SimpleMappingExceptionResolver--> < bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> < property name="warnLogCategory" value="https://www.songbingjia.com/android/warn" /> < property name="defaultStatusCode" value="https://www.songbingjia.com/android/500"/> < property name="defaultErrorView" value="https://www.songbingjia.com/error/500"/> < property name="exceptionMappings"> < props> < prop key="java.lang.Exception"> /error/500< /prop> < /props> < /property> < property name="statusCodes"> < props> < prop key="/error/404"> 404< /prop> < prop key="/error/404"> 400< /prop> < prop key="/error/500"> 500< /prop> < /props> < /property> < /bean> < !--springmvc拦截器mvc:interceptors--> < mvc:interceptors> < mvc:interceptor> < mvc:mapping path="/**"/> < mvc:exclude-mapping path="/static/**" /> < bean class="com.xxx.interceptor.LoginRequiredInterceptor" /> < /mvc:interceptor> < /mvc:interceptors> < /beans>

 
 
 
二、关于applicationContext.xml配置
spring的核心配置文件,也需要在web.xml加载。
 
< ?xml version="1.0" encoding="UTF-8"?> < beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> < !--加载属性数据库文件--> < context:property-placeholder location="classpath:resource/db.properties" /> < !--C3P0连接池:使用:com.mchange.v2.c3p0.ComboPooledDataSource进行配置--> < bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> < !-- 【必须】数据库驱动--> < property name="driverClassName" value="https://www.songbingjia.com/android/${jdbc.driver}" /> < !-- 【必须】 数据库连接地址 --> < property name="url" value="https://www.songbingjia.com/android/${jdbc.url}" /> < !-- 【必须】 数据库用户名 --> < property name="username" value="https://www.songbingjia.com/android/${jdbc.username}" /> < !-- 【必须】 数据库密码 --> < property name="password" value="https://www.songbingjia.com/android/${jdbc.password}" /> < !-- 可选 启动时创建的连接数 --> < property name="initialSize" value="https://www.songbingjia.com/android/5"/> < !-- 可选 同时可从池中分配的最多连接数,0无限制 --> < property name="maxActive" value="https://www.songbingjia.com/android/10"/> < !-- 可选 池中不会被释放的最多空闲连接数 0无限制 --> < property name="maxIdle" value=""/> < !-- 可选 同时能从语句池中分配的预处理语句最大值,0无限制 --> < property name="maxOpenPreparedStatement" value="https://www.songbingjia.com/android/100"/> < !-- 可选 抛异常前池等待连接回收最大时间(当无可用连接),-1无限等待 --> < property name="maxWait" value="https://www.songbingjia.com/android/1000"/> < !-- 可选 连接在池中保持空闲而不被回收的最大时间 --> < property name="minEvictableIdleTimeMillis" value="https://www.songbingjia.com/android/2000"/> < !-- 可选 不创建新连接情况下池中保持空闲的最小连接数 --> < property name="minIdle" value="https://www.songbingjia.com/android/2"/> < !-- 可选 布尔值,是否对预处理语句进行池管理 --> < property name="poolPreparedStatements" value="https://www.songbingjia.com/android/true"/> < /bean> < !--和hibernate的整合--> < bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> < !-- 与dataSource --> < property name="dataSource"> < ref bean="dataSource"/> < /property> < /bean> < !-- 和mybatis的整合 --> < bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> < !-- 数据库连接池 --> < property name="dataSource" ref="dataSource" /> < !-- 加载mybatis的全局配置文件 --> < property name="configLocation" value="https://www.songbingjia.com/android/classpath:mybatis/SqlMapConfig.xml" /> < /bean> < !-- 事务管理器 --> < bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> < !-- 数据源 --> < property name="dataSource" ref="dataSource" /> < /bean> < !-- 配置通知 --> < tx:advice id="txAdvice" transaction-manager="transactionManager"> < tx:attributes> < !-- 传播行为 --> < tx:method name="save*" propagation="REQUIRED" /> < tx:method name="insert*" propagation="REQUIRED" /> < tx:method name="add*" propagation="REQUIRED" /> < tx:method name="create*" propagation="REQUIRED" /> < tx:method name="delete*" propagation="REQUIRED" /> < tx:method name="update*" propagation="REQUIRED" /> < tx:method name="find*" propagation="SUPPORTS" read-only="true" /> < tx:method name="select*" propagation="SUPPORTS" read-only="true" /> < tx:method name="get*" propagation="SUPPORTS" read-only="true" /> < /tx:attributes> < /tx:advice> < !-- 切面 --> < aop:config> < aop:advisor advice-ref="txAdvice" pointcut="execution(* com.store.service.*.*(..))" /> < /aop:config> < !--关于配置文件中的service层的配置。 扫描包下面所有的service层。--> < context:component-scan base-package="com.xxx.service"/> < /beans>

【关于常用的web.xmlapplicationContext.xml与springMVC-servlet.xml】 



    推荐阅读