ctdsy520
ctdsy520
2013-04-15 17:11

springMVC+ehcache 配置没有问题,但是缓存不起作用

已采纳

照着网上的例子用springMVC+ehcache 基于注解的方式 实现缓存功能,配置 没有报错,但是 却不能实现 缓存。 :( 后来 我又用原先成功实现缓存的springAOP拦截器的方式, 也还是不能实现缓存,我觉得 可能是有些冲突或者是被拦截了。
具体代码如下,大家看看吧(这里用了springMVC+mybatis+ehcache):

spring-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
default-lazy-init="true">

<context:annotation-config />

<context:component-scan base-package="com.myword">
    <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan> 


<!-- 载入properties文件 -->
<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:db.properties" />
</bean>


<!-- 配置数据源(使用c3p0连接池 -->
<!-- driverClass : 连接数据库的驱动; jdbcUrl : 连接数据库的URL ; user : 数据库用户名 ; password 
    : 数据库用户密码 ; acquireIncrement : 当数据库缺乏连接时,需要增加多少新连接 ; idleConnectionTestPeriod 
    : 连接在多久未使用之后自动关闭 ; maxPoolSize : 能够创建的最大连接数; maxStatements :一个链接能执行的最大sql语句数 
    ; minPoolSize : 创建的最小连接数 ; -->
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > 
   <property name="driverClassName" value="${db.driverClassName}"> </property> 
   <property name="url" value="${db.url}"> </property> 
   <property name="username" value="${db.username}"> </property> 
   <property name="password" value="${db.password}">  </property>
</bean> -->
<!--  -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="${db.driverClassName}" />
    <property name="jdbcUrl" value="${db.url}" />
    <property name="user" value="${db.username}" />
    <property name="password" value="${db.password}" />
    <property name="acquireIncrement" value="10" />
    <property name="idleConnectionTestPeriod" value="60" />
    <property name="maxPoolSize" value="100" />
    <property name="maxStatements" value="50" />
    <property name="minPoolSize" value="10" />
</bean>


<!-- 配置sqlSessionFactory, 当使用MapperFactoryBean时configLocation没有必要添加 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="configLocation">
        <value>classpath:mybatis.xml</value>
    </property>
    <property name="dataSource" ref="dataSource" />
    <property name="mapperLocations">
        <list>
            <value>classpath:com/myword/mapper/*.xml</value>
        </list>
    </property>
</bean>

<!-- 扫描mapper接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
    <property name="basePackage" value="com.myword.mapper"></property>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<!-- 开启使用注解配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

spring-mvc.xml:
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="com.myword" >
   <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>

<!-- 开启springmvc的注解支持 -->
<mvc:annotation-driven />


<!-- 防止静态资源被过滤器拦截 -->
<mvc:resources mapping="/extjs4/**" location="/extjs4/" />
<mvc:resources mapping="/app/**" location="/app/" />
<mvc:resources mapping="/res/**" location="/res/" />
<mvc:resources mapping="/console.js" location="/console.js" />
<mvc:resources mapping="/app.js" location="/app.js" />

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="utf-8"/>
    <property name="maxUploadSize" value="800000000"/>
</bean>  
<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="ignoreAcceptHeader" value="true" />
    <property name="defaultContentType" value="text/html" />
    <property name="order" value="1" />
    <!--扩展名至mimeType的映射,即 /user.json => application/json-->
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
        </map>
    </property>
    <property name="viewResolvers">
        <list>
             <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
             <bean
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="viewClass"
                    value="org.springframework.web.servlet.view.JstlView" />
                <property name="prefix" value="/" />    
                <property name="suffix" value=".jsp" /> <!--重定向返回默认添加后缀-->
             </bean>
        </list>
    </property>
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
        </list>
    </property>
</bean>


<ehcache:annotation-driven /> 
<ehcache:config cache-manager="cacheManager"> 
    <ehcache:evict-expired-elements interval="60" />
</ehcache:config>

<bean id="cacheManager"
       class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
    <property name="configLocation" >
        <value>classpath:ehcache.xml</value>
    </property>
</bean>

主要的配置 就是这两个,其他导入的 ehcache.xml和mybatis.xml就没有贴出来了。

service 的javabean:
@Service
public class UsersServI implements UsersServ{

@Autowired
private UsersMapper usersMapper;

@Cacheable("baseCache")
public List<Users> getUsersAll(UsersExample example) {
    System.out.println("======ok======");
    return usersMapper.selectByExample(example);
}

action 中的打印处理:
Cache baseCache = manager.getCache("baseCache");
System.out.println("Cache named baseCache:"+baseCache);
System.out.println("============one===============");
UsersExample example = new UsersExample();
usersServ.getUsersAll(example);
System.out.println("============two===============");
usersServ.getUsersAll(example);

结果发现 执行后 后台打印出:
Cache named baseCache:[ name = baseCache status = STATUS_ALIVE eternal = false overflowToDisk = true maxEntriesLocalHeap = 10000 maxEntriesLocalDisk = 0 memoryStoreEvictionPolicy = LRU timeToLiveSeconds = 600000 timeToIdleSeconds = 300000 persistence = none diskExpiryThreadIntervalSeconds = 120 cacheEventListeners: net.sf.ehcache.statistics.LiveCacheStatisticsWrapper hitCount = 0 memoryStoreHitCount = 0 diskStoreHitCount = 0 missCountNotFound = 0 missCountExpired = 0 maxBytesLocalHeap = 0 overflowToOffHeap = false maxBytesLocalOffHeap = 0 maxBytesLocalDisk = 0 pinned = false ]
============one===============
======ok======
============two===============
======ok======

第一个打印"Cache named ..."说明ehcache配置是成功的,ehcache.xml中的cache name 也被打印出来了。
但是 后面getUsersAll方法却被执行了两次 说明 ehcache根本没有起作用。到底哪里出问题了呀?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • jinnianshilongnian jinnianshilongnian 8年前

    你现在使用的是ehcache-spring-annotations
    需要使用 com.googlecode.ehcache.annotations.Cacheable 看看导入的有没有问题

    spring3.1 现在对cache直接支持没必要用这个了 你去搜下 这个可以说过时了

    点赞 评论 复制链接分享
  • jinnianshilongnian jinnianshilongnian 8年前

    你拦截的service 但你把cache配置到mvc的配置文件 能起作用吗 ?

    把cache的放到spring-config.xml:

    点赞 评论 复制链接分享
  • nowdown nowdown 8年前

    我今晚也碰到这样的问题,找了一晚上了,没找到是什么问题。
    之前一样用都是好好的。

    点赞 评论 复制链接分享
  • darrenzhong darren中 7年前

    springMVC+mybatis+spring+ehcache 可以看看这篇文章 原创 http://darrenzhong.com/?p=348 有问题留言在线解答

    点赞 评论 复制链接分享

相关推荐