为了账号安全,请及时绑定邮箱和手机立即绑定

JCache:指定的缓存键类型不兼容

JCache:指定的缓存键类型不兼容

HUWWW 2021-08-19 21:11:40
我在 Spring Boot 中配置缓存时遇到问题。它工作正常,经过一些无关的更改后,它停止工作。我有以下缓存配置:@Configurationpublic class UserMappingCacheConfig {    public static final String USER_MAPPING_CACHE = "userMappingCache";@Bean(name = "userMappingCache")public Cache<String, String> getUserMappingCache(JCacheCacheManager cacheManager) {    CacheManager cm = cacheManager.getCacheManager();    Cache<String, String> cache = cm.getCache(USER_MAPPING_CACHE, String.class, String.class);    if (cache == null)        cache = cm.createCache(USER_MAPPING_CACHE, getUserMappingCacheConfiguration());    return cache;}private MutableConfiguration<String, String> getUserMappingCacheConfiguration() {    MutableConfiguration<String, String> configuration =            new MutableConfiguration<String, String>()                    .setStoreByValue(true)                    .setExpiryPolicyFactory( FactoryBuilder.factoryOf(                            new CreatedExpiryPolicy( Duration.ONE_DAY)                    ));    return configuration;}我按以下方式使用缓存:@CacheResult(cacheName = "userMappingCache")public String getPayrollUserName(@CacheKey String userName, String description) {...}但是,运行服务时出现以下异常:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMappingCache' defined in class path resource [UserMappingCacheConfig.class]: Bean instantiation via factory method failed;nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.Cache]: Factory method 'getUserMappingCache' threw exception; nested exception is java.lang.ClassCastException: Incompatible cache key types specified, expected class java.lang.Object but class java.lang.String was specified                at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]    ....我搜索了一下,发现了一些条目,这些条目主要与键/值的序列化类似的问题有关。即使是原始类也是这个原因吗?我该如何解决?提前致谢。
查看完整描述

1 回答

?
万千封印

TA贡献1891条经验 获得超3个赞

从我的评论来看,似乎答案MutableConfiguration是没有使用

setType(Class<K>,Class<V>)

这将配置缓存的键和值类型。如果没有,它会默认为Object,这是与(隐含的)类型的不兼容String@CacheKey/@CacheResult配对听写。


查看完整回答
反对 回复 2021-08-19
  • 1 回答
  • 0 关注
  • 305 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信