java - JCache:指定了不兼容的缓存键类型,需要类 java.lang.Object,但类 java.lang.String

标签 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]

我用谷歌搜索了一下,发现了一些条目,这些条目大多与键/值的序列化相关。即使对于原始类,这也是原因吗?我该如何修复它? 提前致谢。

最佳答案

从我的评论来看,答案似乎是 MutableConfiguration 没有使用

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

这将配置缓存的键和值类型。如果没有它,它将默认为 Object,并且这与 @CacheKey/@ 的 (隐含) String 类型不兼容CacheResult 配对指令。

关于java - JCache:指定了不兼容的缓存键类型,需要类 java.lang.Object,但类 java.lang.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52086423/

相关文章:

java - 在另一个 jsp 中使用一个 jsp 中的函数

java - 如何在 Spring Boot 中重写InternalResourceViewResolver?

oracle - 在 Liquibase sql 变更集中获取项目根目录

hazelcast - java.lang.NoSuchMethodError : com. google.common.util.concurrent.MoreExecutors.sameThreadExecutor

java - 将数据放入 map 时 Hazelcast、HazelcastSerializationException

java - 与jProgressBar同步复制显示

java - 验证实例是否在 AWS 中运行的安全方法

java - OntouchListener 向数组添加太多值

spring-boot - 使用spring boot,它如何检测JTA环境?

java - 榛子 : changing configuration programatically doesnt work