JCache:指定了不兼容的缓存键类型,应该是类java.lang.Object,但是是类java.lang.String

时间:2018-08-29 21:38:36

标签: java spring-boot hazelcast jcache

我在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]
    ....
    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]

我到处搜索,发现一些条目,这些条目大多与键/值的序列化有关。那就是使用原始类的原因吗?我该如何解决? 预先感谢。

1 个答案:

答案 0 :(得分:1)

根据我的评论,似乎答案是MutableConfiguration没有使用

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

,它将配置缓存的键和值类型。没有它们,它将默认为Object,这与String / @CacheKey配对所规定的@CacheResult类型(暗示)不兼容。