亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 325 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號