1.配置SpringMVC核心配置文件:SpringMVC-servlet.xml。配置关于项目的组件。
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: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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- 启动扫描所有的controller -->
<context:component-scan base-package="com.xwtec.mss.work.controller" />
<context:component-scan base-package="com.xwtec.mss.work.webservice" />
<!-- 配置拦截器 -->
<mvc:interceptors>
<!-- 对request请求(除了登录页面,首页,异常处理页面请求)需要做token验证 -->
<mvc:interceptor>
<mvc:mapping path="/maintain/**"/>
<bean class="com.xwtec.mss.work.common.aspect.AccessValidationInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/search/showDic"/>
<bean class="com.xwtec.mss.work.common.aspect.AccessValidationInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 静态资源访问时交由默认Servlet处理,不走mapping映射 -->
<mvc:default-servlet-handler/>
<!-- 避免IE在ajax请求时,返回json出现下载 -->
<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 文件上功能 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760000" />
<property name="maxInMemorySize" value="40960" />
</bean>
<!-- 对静态资源文件的访问,配置js,css等静态文件直接映射到对应的文件夹,不被DispatcherServlet处理 mapping,
WebRoot里面的静态文件位置目录 location, 访问地址 -->
<mvc:resources location="/resources/**" mapping="/WEB-INF/resources/" cache-period="31556926"/>
<!-- 视图解释类,jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在/WEB-INF/views/目录下,查找XXX.jsp文件 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
2.配置springMVC加载属性文件以及其他配置文件(包括关于mybatis)
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 加载属性文件以及其他配置文件 -->
<bean id="propertyconfig"
class="com.xwtec.mss.work.common.config.CustomizedPropertyConfigurer">
<property name="fileEncoding" value="UTF-8"></property>
<property name="locations">
<list>
<value>classpath:config.properties</value>
<value>classpath:ws.properties</value>
</list>
</property>
</bean>
<bean id="springBeanInvoker" class="com.xwtec.mss.work.util.SpringBeanInvoker" />
<import resource="applicationContext-db.xml" />
<import resource="applicationContext-aspect.xml" />
<import resource="applicationContext-task.xml" />
</beans>
3.配置关于springMVC和mybatis的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
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-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 数据源配置
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${jndiDataSource_workbench}" />
</bean>
-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.connection.driverClass}" />
<property name="url" value="${jdbc.connection.url}" />
<property name="username" value="${jdbc.connection.username}" />
<property name="password" value="${jdbc.connection.password}" />
<property name="initialSize" value="${jdbc.connection.initialSize}" />
<property name="minIdle" value="${jdbc.connection.minIdle}" />
<property name="maxIdle" value="${jdbc.connection.maxIdle}" />
<property name="maxActive" value="${jdbc.connection.maxActive}" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.connection.timeBetweenEvictionRunsMillis}" />
<!-- 对于长时间不使用的连接强制关闭 -->
<property name="removeAbandoned" value="${jdbc.connection.removeAbandoned}" />
<!-- 超过**秒开始关闭空闲连接 -->
<property name="removeAbandonedTimeout" value="${jdbc.connection.removeAbandonedTimeout}" />
</bean>
<!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
</bean>
<!-- 定义 SqlSessionTemplate模版 可以批处理的 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 声明式事务管理器 -->
<aop:config>
<aop:pointcut id="transactionPointcut"
expression="execution(* com.xwtec.mss.work.service..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut"
advice-ref="transactionAdvice" />
</aop:config>
<!-- 事务拦截器 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" timeout="120" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="save*" timeout="120" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="insert*" timeout="120" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="update*" timeout="120" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="modify*" timeout="120" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="edit*" timeout="120" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="delete*" timeout="120" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="remove*" timeout="120" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="*Add" timeout="120" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="*Update" timeout="120" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="*Del" timeout="120" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="*" timeout="120" propagation="SUPPORTS"
rollback-for="Exception" />
</tx:attributes>
</tx:advice>
<!-- 自动扫描组件,需要把controller去掉,否则影响事务管理 -->
<context:component-scan base-package="com.xwtec.mss.work">
<context:exclude-filter type="regex"
expression="com.xwtec.mss.work.controller.*" />
</context:component-scan>
</beans>
最后,添加关于数据库连接的属性文件即可。