我在 Spring Boot 中配置缓存时遇到问题。它工作正常,经过一些无关的更改后,它停止工作。
我有以下缓存配置:
@Configuration
public 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]
....
我搜索了一下,发现了一些条目,这些条目主要与键/值的序列化类似的问题有关。即使是原始类也是这个原因吗?我该如何解决?提前致谢。
万千封印
相关分类