汽车之家网页版官网找车/宁波seo网站排名优化公司
弄了一大早,终于配好了事务,事务的扫描包配好,Ehcache就是切面的问题,一切问题也迎刃而解。。。
一、Spring事务回滚
1、applicationContext.xml中配置
<!--spring 扫包 @Service ..... --><context:component-scan base-package="com.yanhui"><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" /></context:component-scan>
2、spring-mvc.xml配置
<!-- 扫描器--><!-- <context:component-scan base-package="com.yanhui.*" /> --><context:component-scan base-package="com.yanhui" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>
3、applicationContext.xml开启事务
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- <tx:annotation-driven transaction-manager="transactionManager"/> --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="publish*" /><tx:method name="save*" /><tx:method name="add*" /><tx:method name="update*" /><tx:method name="insert*" /><tx:method name="create*" /><tx:method name="del*" /><tx:method name="load*" /><tx:method name="init*" /><tx:method name="*" read-only="true"/></tx:attributes></tx:advice><!-- AOP配置 --><aop:config><aop:pointcut id="myPointcut"expression="execution(public * com.yanhui.service..*.*(..))" /><aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" /></aop:config>
4、测试
mapper.insert(record);
int a = 1/0;
二、Ehcache配置
1、引入jar包
<dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache-core</artifactId><version>2.6.11</version></dependency>
2、新建一个cache-config.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:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd "><cache:annotation-driven cache-manager="cacheManager"/><bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation" value="classpath:ehcache.xml"/></bean><bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"><property name="cacheManager" ref="ehcacheManager"/><property name="transactionAware" value="true"/></bean>
</beans>
3、新建一个ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"updateCheck="false"><diskStore path="java.io.tmpdir"/><defaultCache eternal="false"maxEntriesLocalHeap="1000"overflowToDisk="false"diskPersistent="false"timeToIdleSeconds="3600"timeToLiveSeconds="3600"/><cache name="baseCache"eternal="false"maxEntriesLocalHeap="200"overflowToDisk="false"diskPersistent="false"timeToIdleSeconds="600"statistics="true"timeToLiveSeconds="600"/><!--eternal="false" // 元素是否永恒,如果是就永不过期(必须设置)maxElementsInMemory="1000" // 缓存容量的内存最大值(必须设置)overflowToDisk="false" // 当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置)diskPersistent="false" // 磁盘缓存在VM重新启动时是否保持(默认为false)timeToIdleSeconds="0" // 导致元素过期的访问间隔(秒为单位). 0表示可以永远空闲,默认为0timeToLiveSeconds="600" // 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期memoryStoreEvictionPolicy="LFU" // 当达到maxElementsInMemory时,如何强制进行驱逐默认使用"最近使用(LRU)"策略,其它还有先入先出FIFO,最少使用LFU,较少使用LRU-->
</ehcache>
4、applicationContext.xml中加入
<import resource="cache-config.xml"/>
5、service.impl方法中
@Cacheable(value = "baseCache")public User findById(int id) {return userMapper.selectByPrimaryKey(id);}
6、测试
三、遇到的坑
spring、springMVC重复扫描导致事务失效的问题
意思是applicationContext.xml中扫描包和spring-mvc中的扫描包重复了,前者作为父容器先启动并配置了事务,然后spring-mvc.xml再启动,作为子容器也来管理包的内容,但是会覆盖前面applicationContext.xml对包的处理,而新的管理中并没有配置事务,所以事务就失败了。
经结论得出:
由于服务器启动时的加载配置文件的顺序为web.xml—root-context.xml(Spring的配置文件)—servlet-context.xml(SpringMVC的配置文件)。
解决办法:
Spring容器中不扫描Controller上的注解(即SpringMVC注解),SpringMVC层只负责扫描@Controller