spring和springmvc不需要整合。这里分成两个xml,一个是springmvc.xml,一个是spring-mybatis.xml。
这里是配置文件的位置
springmvc.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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> <context:component-scan base-package="com.haobai"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <!--过滤静态资源 --> <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources> <!-- <mvc:resources location="/html/" mapping="/html/**"></mvc:resources> --> <!-- 换成UTF-8编码 --> <bean id="utf8Charset" class="java.nio.charset.Charset" factory-method="forName"> <constructor-arg value="UTF-8"/> </bean> <!-- 引入注解驱动 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg ref="utf8Charset"/> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 定义跳转的文件的前后缀 ,视图模式配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 --> <property name="prefix" value="views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 启用spring对AspectJ注解的支持 ,放在另一个xml中无效--> <aop:aspectj-autoproxy proxy-target-class="true"/> <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 --> <cache:annotation-driven cache-manager="ehcacheManager"/> <!-- cacheManager工厂类,指定ehcache.xml的位置 --> <bean id="ehcacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> </bean> <!-- 声明cacheManager --> <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehcacheManagerFactory" /> </bean> <!-- 跨域 --> <mvc:cors> <mvc:mapping path="/**"/> </mvc:cors> <!--session拦截 --> <!-- <mvc:interceptors> <mvc:interceptor> 匹配的是url路径, 如果不配置或/**,将拦截所有的Controller <mvc:mapping path="/**"/> 不拦截登录请求 <mvc:exclude-mapping path="/login"/> <bean class="com.haobai.handlerInterceptor.SessionInterceptor" /> </mvc:interceptor> </mvc:interceptors> --> </beans>
spring-mybati<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!--1.自动扫描 将标注Spring注解的类自动转化Bean --> <!-- 扫描注解Bean --> <context:component-scan base-package="com.haobai"> <!-- 排除含@Controller注解的类 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 定时任务 --> <task:annotation-driven scheduler="scheduler" mode="proxy"/> <task:scheduler id="scheduler" pool-size="10"/> <!--2.加载数据资源属性文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> <!--要是有多个配置文件,只需在这里继续添加即可 --> <value>classpath:redis.properties</value> </list> </property> </bean> <!-- 3.配置数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="1" /> <property name="minIdle" value="3" /> <property name="maxActive" value="50" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="30000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="testWhileIdle" value="true" /> <!-- 这里建议配置为TRUE,防止取到的连接不可用 --> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="validationQuery" value="SELECT 'x'" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小。如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。分库分表较多的数据库,建议配置为false。 --> <property name="poolPreparedStatements" value="false" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <property name="filters" value="stat" /> </bean> <!-- 4.配置sessionfactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/haobai/mapper/*.xml"></property> </bean> <!-- 5.装配mapper接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.haobai.mapper" /> <!-- mapper接口所在包名,Spring会自动查找其下的类 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 6.声明式事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 在service层使用Transcational注解方式配置事务,不能使用trycatch语句,失败则回滚 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>redis+spring实现session共享>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> --> <!-- jedis 配置 --> <!-- <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="600"/> </bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="100" /> <property name="maxIdle" value="10" /> </bean> redis服务器中心 <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"> <property name="hostName" value="${redis_hostname}"/> <property name="port" value="${redis_port}"/> <property name="password" value="${redis_pwd}" /> <property name="timeout" value="3000"/> <property name="usePool" value="true"/> <property name="poolConfig" ref="jedisPoolConfig"/> </bean> --> </beans>
配置文件里还配置了定时任务、拦截器、redis和redis下的session共享。下面是web.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <!-- redis+spring实现session共享 --> <!-- <filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> --> <filter> <!-- 过滤器名称 --> <filter-name>SessionFilter</filter-name> <!-- 过滤器类的包路径 --> <filter-class>com.haobai.filter.SessionFilter</filter-class> <!-- 可选 --> <init-param> <!-- 过滤器初始化参数 --> <param-name>excludedPages</param-name> <param-value>/login.jsp</param-value> </init-param> </filter> <!--过滤器映射 --> <filter-mapping> <filter-name>SessionFilter</filter-name> <!-- 指定过滤器作用的对象 --> <url-pattern>*.jsp</url-pattern> </filter-mapping> <!-- <filter-mapping> <filter-name>SessionFilter</filter-name>与上面指定的filter的filter-name一致 <url-pattern>*.do</url-pattern>指定过滤的路径2 </filter-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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- SpringMVC前端控制器 --> <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.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <!--解决ehcache内存溢出 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- log4j配置,文件路径,因为是跟随项目启动 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <!-- 配置log4j.xml变量,如果需要动态的就使用下面方式,使用方法${name} --> <context-param> <param-name>controller</param-name> <param-value>controller-log</param-value> </context-param> <context-param> <param-name>loggingLevel</param-name> <param-value>info</param-value> </context-param> <!-- 加载log4j配置文件 --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!--如果某个会话在一定时间未被访问,则服务器可以扔掉以节约内存(单位:分钟) --> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app>
需要注意的是,spring-mybtis的文件因为是用的listener,所以在项目启动的时候就加载了,而springmvc的配置文件用的是servlet。web.xml 的加载顺序是:context-param -> listener -> filter -> servlet ,而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的。
本人亲测,将定时任务放在springmvc.xml中会导致在有请求之后定时任务才会被触发,这是个坑,切记。。。
然后最后要说的是,aop、定时任务、事务管理,这里面都是用的注解的方式实现的