springmvc+redis缓存问题(添加@Cacheable无效问题)

来源:互联网 发布:农业科技网络书屋网站 编辑:程序博客网 时间:2024/06/07 20:46

搭建时,遇到@Cacheable等注释无效,未将数据存入redis中,现将问题记录下解决方案:

主要的相关配置如下:

在applicationContext.xml中配置了redis相关配置,

  <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->    
    <cache:annotation-driven /> 

<context:property-placeholder location="classpath:config/redis.properties"/>
          <!-- jedis 配置 -->  
   <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
       <property name="maxIdle" value="${redis.maxIdle}" />  
       <!--<property name="maxWaitMillis" value="${redis.maxWait}" />-->  
       <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
   </bean>  
  
   <!-- redis服务器中心 -->  
   <bean id="connectionFactory"  
       class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
       <property name="poolConfig" ref="poolConfig" />  
       <property name="port" value="${redis.port}" />  
       <property name="hostName" value="${redis.hostname}" />  
       <!-- <property name="password" value="${redis.password}" /> -->  
       <property name="timeout" value="${redis.timeout}"></property>  
   </bean>  
   
   <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
       <property name="connectionFactory" ref="connectionFactory" />  
       <property name="keySerializer">  
           <bean  
               class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
       </property>  
       <property name="valueSerializer">  
           <bean  
               class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
       </property>  
   </bean>  
          <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
          <property name="caches">
          <set>
          <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                    <property name="name" value="default"></property> 
                    </bean>
                     <bean class="com.hjc.util.RedisCache">   
                    <property name="redisTemplate" ref="redisTemplate" />   
                    <property name="name" value="data"/>   
                     <!-- common名称要在类或方法的注解中使用 --> 
                     </bean>
          </set>
          </property>
          </bean>
          

在spring-Servlet.xml中配置:

<mvc:default-servlet-handler/>
<mvc:annotation-driven />
<context:component-scan base-package="com.hjc.*" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
</bean>


解决办法:

将applicationContext.xml中

  <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->    
    <cache:annotation-driven /> 

剪切粘贴到spring-Servlet.xml文件中或者将spring-Servlet.xml文件中
<mvc:annotation-driven />
<context:component-scan base-package="com.hjc.*" />

剪贴拷贝到applicationContext.xml文件中。源码下载如下:
原创粉丝点击