Spring MVC XML文件配置示例
Eave
2016.08.21
web.xml文件配置示例
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>account.gramess.com</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath: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>
<servlet>
<servlet-name>DruidStatView</servlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DruidStatView</servlet-name>
<url-pattern>/druid/*</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>
<session-config>
<session-timeout>240</session-timeout>
</session-config>
</web-app>
springmvc-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 支持异步方法执行 异步方法中加上注解 @Async -->
<task:annotation-driven></task:annotation-driven>
<!-- springmvc注解驱动 -->
<mvc:annotation-driven>
<!-- 在匹配模式时是否使用后缀模式匹配 -->
<mvc:path-matching suffix-pattern="false"></mvc:path-matching>
<mvc:message-converters register-defaults="false">
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="features">
<array value-type="com.alibaba.fastjson.serializer.SerializerFeature">
<value>DisableCircularReferenceDetect</value>
</array>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- springmvc注解驱动 -->
<mvc:annotation-driven>
<!-- 在匹配模式时是否使用后缀模式匹配 -->
<mvc:path-matching suffix-pattern="false"></mvc:path-matching>
</mvc:annotation-driven>
<!-- 自动扫描(自动注入) -->
<context:component-scan base-package="com.qianyan"></context:component-scan>
<!-- 导入 属性文件 -->
<context:property-placeholder location="classpath:*.properties"></context:property-placeholder>
<!-- 静态资源处理 -->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!-- 静态文件 -->
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
<mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
<mvc:resources mapping="/fonts/**" location="/fonts/"></mvc:resources>
<!-- 注册拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 被拦截的范围 -->
<mvc:mapping path="/**"></mvc:mapping>
<!-- 不被拦截的范围 -->
<mvc:exclude-mapping path="/login"></mvc:exclude-mapping>
<mvc:exclude-mapping path="/login/**"></mvc:exclude-mapping>
<mvc:exclude-mapping path="/qrcode/**"></mvc:exclude-mapping>
<mvc:exclude-mapping path="/notify/**"></mvc:exclude-mapping>
<bean class="com.qianyan.interceptors.LoginInterceptors"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/View/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 数据连接信息 -->
<property name="url" value="${jdbc.jdbcUrl}"></property>
<!-- 数据库用户 -->
<property name="username" value="${jdbc.user}"></property>
<!-- 数据库密码 -->
<property name="password" value="${jdbc.password}"></property>
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1"></property>
<property name="minIdle" value="1"></property>
<property name="maxActive" value="20"></property>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000"></property>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"></property>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000"></property>
<property name="validationQuery" value="SELECT 'x'"></property>
<property name="testWhileIdle" value="true"></property>
<property name="testOnBorrow" value="false"></property>
<property name="testOnReturn" value="false"></property>
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true"></property>
<property name="maxPoolPreparedStatementPerConnectionSize" value="20"></property>
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat"></property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/qianyan/mapping/*.xml"></property>
<property name="configLocation" value="classpath:mybatis.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 org.mybatis.spring.sample.mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qianyan.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 注册自动获取Bean的类 -->
<bean id="springWebContextUtils" class="com.qianyan.util.SpringWebContext"></bean>
<!-- Bean -->
<!-- Redis连接池 -->
<bean id="redisBean" class="com.qianyan.beans.RedisBean">
<property name="host" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="maxIdle" value="${redis.maxIdle}"></property>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>
<property name="onBorrow" value="${redis.onBorrow}"></property>
<property name="onReturn" value="${redis.onReturn}"></property>
</bean>
<bean id="persion" class="com.qianyan.beans.Persion">
<property name="name" value="钱焱"></property>
<property name="mobile" value="18565424682"></property>
<property name="birthday" value="1993.11.20"></property>
<property name="address" value="重庆市奉节县甲高镇甲高中学"></property>
<property name="idNo" value="500236199311203961"></property>
</bean>
</beans>
mybatis.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>
<!-- mybatis支持属性使用驼峰的命名 -->
<setting name="mapUnderscoreToCamelCase" value="true"></setting>
</settings>
</configuration>