【发布时间】:2016-04-07 06:51:21
【问题描述】:
我在从缓存中获取价值时遇到问题。
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
值正在被缓存,但在获取应用程序时抛出类转换错误。可能的问题是什么。
【问题讨论】:
-
你的类路径中有两次相同的
class吗?或者您是否从多个类加载器(或 WebApp 环境中的示例)加载它。无法将类转换为自身问题的通常原因是类是从不同的地方加载的...... -
猜测,它看起来像是某种 ClassLoader 问题。看起来你有两个不同的类加载器,它们加载了同一个类。
-
@sisyphus 我使用 spring boot + devtools。我读过一些 devtools 为静态 jars 保留一个类加载器和一个用于应用程序代码的地方。这会导致问题吗?
-
@BoristheSpider 是的,我删除了 spring-boot-devtools 及其工作。谢谢各位
-
我应该提出有关 spring-boot 的问题吗?根据他们的指导方针,他们正在观看此标签。另外我不认为这是可以解决的,请提出建议。
标签: java spring-boot memcached jhipster spring-cache