JCache:指定的缓存键类型不兼容,预期 class java.lang.Object 但 class java.lang.String
JCache: Incompatible cache key types specified, expected class java.lang.Object but class java.lang.String
我在 Spring 启动时配置缓存时遇到问题。它工作正常,但经过一些不相关的更改后,它停止工作了。
我有以下缓存配置:
@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]
....
Caused by: 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.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
....
Caused by: java.lang.ClassCastException: Incompatible cache key types specified, expected class java.lang.Object but class java.lang.String was specified
at com.hazelcast.cache.impl.AbstractHazelcastCacheManager.getCache(AbstractHazelcastCacheManager.java:200) ~[hazelcast-3.10.jar:3.10]
at com.hazelcast.cache.impl.AbstractHazelcastCacheManager.getCache(AbstractHazelcastCacheManager.java:67) ~[hazelcast-3.10.jar:3.10]
我四处搜索,发现一些条目大多与 keys/values 的序列化相关。这就是原始 class 的原因吗?我该如何解决?
提前致谢。
根据我的评论,答案似乎是 MutableConfiguration
没有使用
setType(Class<K>,Class<V>)
配置缓存的键值类型。没有它,它将默认为 Object
,这与 @CacheKey
/@CacheResult
配对指示的 String
的(隐含)类型不兼容。
我在 Spring 启动时配置缓存时遇到问题。它工作正常,但经过一些不相关的更改后,它停止工作了。
我有以下缓存配置:
@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]
....
Caused by: 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.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
....
Caused by: java.lang.ClassCastException: Incompatible cache key types specified, expected class java.lang.Object but class java.lang.String was specified
at com.hazelcast.cache.impl.AbstractHazelcastCacheManager.getCache(AbstractHazelcastCacheManager.java:200) ~[hazelcast-3.10.jar:3.10]
at com.hazelcast.cache.impl.AbstractHazelcastCacheManager.getCache(AbstractHazelcastCacheManager.java:67) ~[hazelcast-3.10.jar:3.10]
我四处搜索,发现一些条目大多与 keys/values 的序列化相关。这就是原始 class 的原因吗?我该如何解决? 提前致谢。
根据我的评论,答案似乎是 MutableConfiguration
没有使用
setType(Class<K>,Class<V>)
配置缓存的键值类型。没有它,它将默认为 Object
,这与 @CacheKey
/@CacheResult
配对指示的 String
的(隐含)类型不兼容。