zoukankan      html  css  js  c++  java
  • org.springframework.cache.interceptor.SimpleKey cannot be cast to java.lang.String

    springboot整合redis时,使用@Cacheable注解,如果方法的key参数为空,就会报org.springframework.cache.interceptor.SimpleKey cannot be cast to java.lang.String的错误。

    ♛ 1 错误信息

    org.springframework.cache.interceptor.SimpleKey cannot be cast to java.lang.String

    ♛ 2 如图

    org.springframework.cache.interceptor.SimpleKey cannot be cast to java.lang.String

    ♛ 3 解决方案
    package com.test.config;
    
    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.core.RedisTemplate;
    
    /**
     * Created by toutou on 2019/3/9.
     */
    @Configuration
    @EnableCaching
    public class RedisCacheConfig extends CachingConfigurerSupport {
    
        @Override
        @Bean
        public KeyGenerator keyGenerator() {
            return (target, method, 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();
            };
        }
    
        @Bean
        public CacheManager cacheManager(RedisTemplate redisTemplate) {
            RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
            // 设置默认过期时间为60秒
            cacheManager.setDefaultExpiration(60);
            return cacheManager;
        }
    }


    作  者:请叫我头头哥
    出  处:http://www.cnblogs.com/toutou/
    关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!
    版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
    特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信
    声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!

  • 相关阅读:
    Silverlight2 控件布局(笔记)
    Silverligth动态控件例子
    Silverlight中的DataGrid绑定数据
    SilverLight学习笔记对象数据绑定
    得到DataGrid的某一行列的值(转载)
    Silverlight数据绑定简单例子
    SilverLight学习笔记XML操作
    SilverLight学习笔记本地(客户端)数据存储
    SilverLight学习笔记WebClient异步请求
    SilverLight学习笔记泛型数据绑定
  • 原文地址:https://www.cnblogs.com/toutou/p/10516440.html
Copyright © 2011-2022 走看看