本文介绍了spring-boot-devtools从缓存中获取ClassCastException。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从缓存中获取值时我遇到问题。
I am facing issue while getting value from cache.
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-spring-memcached 3.5.0
I am using com.google.code.simple-spring-memcached 3.5.0
值正在缓存但是在获取应用程序时会抛出类强制转换错误。什么是可能的问题。
Value is getting cached but while getting application throws class cast error. What would be the possible issues.
推荐答案
这是。反序列化缓存条目时,该对象未附加到正确的类加载器。
This is a known limitation of Devtools. When the cache entry is deserialized, the object is not attached to the proper classloader.
有多种方法可以解决此问题:
There are various ways you can fix this issue:
- 在开发中运行应用程序时禁用缓存
- 使用其他缓存管理器(如果您使用的是Spring Boot 1.3) ,您可以使用
应用程序中的
并在IDE中启用dev配置文件)spring.cache.type
属性强制使用简单
缓存管理器-dev.properties - 将memcached(以及缓存的内容)配置为。我不推荐这个选项,因为上面的两个更容易实现
- Disable cache when you're running your application in development
- Use a different cache manager (if you're using Spring Boot 1.3, you could force a
simple
cache manager using thespring.cache.type
property inapplication-dev.properties
and enable the dev profile in your IDE) - Configure memcached (and things that are cached) to run in the application classloader. I wouldn't recommend that option since the two first above are much easier to implement
这篇关于spring-boot-devtools从缓存中获取ClassCastException。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!