BullWark
2017-11-25 00:47
采纳率: 100%
浏览 7.8k

Redis新手。cache配置类问题

The constructor RedisCacheManager(RedisTemplate) is undefined

redis新手。第一次用。项目是Springboot

import java.lang.reflect.Method;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
@EnableCaching
public class RedisConfig  extends CachingConfigurerSupport{
     @Bean
        public KeyGenerator keyGenerator() {
            return new KeyGenerator() {
                @Override 
                public Object generate(Object target, Method method, Object... params) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(target.getClass().getName());
                    sb.append(method.getName());
                    for (Object obj : params) {
                        sb.append(obj.toString());
                    }
                    return sb.toString();
                }
            };
        }

        @SuppressWarnings("rawtypes")
        @Bean
        public CacheManager cacheManager(RedisTemplate redisTemplate) {
                    //问题在这里
            RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
            rcm.setDefaultExpiration(60);//秒
            return rcm;
        }

        @Bean
        public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
            StringRedisTemplate template = new StringRedisTemplate(factory);
            Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
            ObjectMapper om = new ObjectMapper();
            om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            jackson2JsonRedisSerializer.setObjectMapper(om);
            template.setValueSerializer(jackson2JsonRedisSerializer);
            template.afterPropertiesSet();
            return template;
        }
}


是包引错了么。还是这个写法不对。

 <dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-redis</artifactId>  
    <version>1.4.7.RELEASE</version>
</dependency>  
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • Fallen_Devil 2018-04-03 00:51
    已采纳

    这个问题是由于使用了spring boot 2.0的问题,在spring boot 2.0中RedisCacheManager,没有

     @SuppressWarnings("rawtypes")
        public RedisCacheManager(RedisOperations redisOperations) {
            this(redisOperations, Collections.<String> emptyList());
        }
    

    这个实现了。

    点赞 打赏 评论
  • DongVagrant 2017-11-25 06:15

    update一下maven项目

    不行的话在maven库中删除spring-boot-starter-redis这个包,然后再update一下maven项目

    点赞 打赏 评论
  • 慵懒的被子 2018-06-30 07:30

    问一下,这个怎么解决的

    点赞 打赏 评论
  • JavaCode猿 2018-08-03 14:20
    点赞 打赏 评论

相关推荐 更多相似问题