如何解决spring-boot-devtools从缓存获取时导致ClassCastException
这是Devtools的已知限制。反序列化高速缓存条目时,该对象未附加到正确的类加载器。
您可以通过多种方式解决此问题:
- 在开发中运行应用程序时禁用缓存
- 使用其他缓存管理器(如果您使用的是Spring Boot 1.3,则可以
simple
使用中的spring.cache.type
属性强制使用缓存管理器application-dev.properties
并在IDE中启用dev配置文件) - 配置memcached(以及缓存的内容)以在应用程序类加载器中运行。我不建议使用该选项,因为上面的前两个更容易实现
解决方法
我在从缓存中获取价值时面临问题。
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
值正在缓存,但在获取应用程序时会抛出类强制转换错误。可能是什么问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。