org.springframework.cache.interceptor.SimpleKey cannot be cast to java.lang.String
Posted toutou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 如图
? 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; } }
以上是关于org.springframework.cache.interceptor.SimpleKey cannot be cast to java.lang.String的主要内容,如果未能解决你的问题,请参考以下文章
org.springframework.cache.caffeine.CaffeineCacheManager 找不到解决
ssh整合context:component-scan包名写了*号:Failed to parse configuration class [org.springframework.cache.asp