我在从缓存中获取价值时面临问题。spring-boot-devtools在从缓存中获取时导致ClassCastException。
java.lang.RuntimeException: java.lang.ClassCastException: com.mycom.admin.domain.User cannot be cast to com.mycom.admin.domain.User
缓存配置
@Configuration
@EnableCaching
@AutoConfigureAfter(value = { MetricsConfiguration.class, DatabaseConfiguration.class })
@Profile("!" + Constants.SPRING_PROFILE_FAST)
public class MemcachedCacheConfiguration extends CachingConfigurerSupport {
private final Logger log = LoggerFactory.getLogger(MemcachedCacheConfiguration.class);
@Override
@Bean
public CacheManager cacheManager() {
ExtendedSSMCacheManager cacheManager = new ExtendedSSMCacheManager();
try {
List<SSMCache> list = new ArrayList<>();
list.add(new SSMCache(defaultCache("apiCache"), 86400, false));
cacheManager.setCaches(list);
} catch (Exception e) {
e.printStackTrace();
}
return cacheManager;
}
@Override
public CacheResolver cacheResolver() {
return null;
}
@Override
public CacheErrorHandler errorHandler() {
return null;
}
private Cache defaultCache(String cacheName) throws Exception {
CacheFactory cacheFactory = new CacheFactory();
cacheFactory.setCacheName(cacheName);
cacheFactory.setCacheClientFactory(new MemcacheClientFactoryImpl());
String serverHost = "127.0.0.1:11211";
cacheFactory.setAddressProvider(new DefaultAddressProvider(serverHost));
cacheFactory.setConfiguration(cacheConfiguration());
return cacheFactory.getObject();
}
@Bean
public CacheConfiguration cacheConfiguration() {
CacheConfiguration cacheConfiguration = new CacheConfiguration();
cacheConfiguration.setConsistentHashing(true);
return cacheConfiguration;
}
}
而且随着
@Cacheable(value = "apiCache#86400", key = "'User-'.concat(#login)")
注释我使用com.google.code.simple弹簧-memcached的3.5.0
价值越来越缓存但获取应用程序时抛出类抛出错误。什么是可能的问题。
你在类路径上有两次相同的'class'吗?或者你是否已经从多个类加载器(或WebApp环境中的示例)加载它。一个类的通常原因不能被转换为它自己的问题是,类是从不同的地方加载... –
在猜测,它看起来像某种类加载器问题。看起来你有两个不同的加载了相同类的类加载器。 – sisyphus
@sisyphus我使用spring boot + devtools。我读了一些devtools在哪里为静态jar保留一个类加载器,以及一个用于应用程序代码。这会引起问题吗? – titogeo