我在從緩存中獲取價值時面臨問題。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