我在 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
配對聽寫。
添加回答
舉報
0/150
提交
取消