Java spring引导devtools在从缓存获取时导致ClassCastException。
我在从缓存获取值时遇到问题Java spring引导devtools在从缓存获取时导致ClassCastException。,java,spring-boot,memcached,jhipster,spring-cache,Java,Spring Boot,Memcached,Jhipster,Spring 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, DatabaseCo
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;
}
}
我正在使用com.google.code.simple-spring-memcached 3.5.0
值正在缓存,但获取应用程序时抛出类强制转换错误。可能的问题是什么
这是我的。当缓存项被反序列化时,对象不会附加到适当的类加载器
有多种方法可以解决此问题:
Spring.cache.type
属性在application-dev.properties
中强制使用simple
缓存管理器,并在IDE中启用开发配置文件)我也犯了同样的错误,但缓存不是原因。事实上,我使用的是缓存,但是注释缓存没有帮助 根据这里和那里的提示,我刚刚介绍了我的对象的附加序列化/派生。这无疑是最好的方法(性能问题),但它正在发挥作用 因此,对于其他人,我更改了代码:
@Cacheable("tests")
public MyDTO loadData(String testID) {
// add file extension to match XML file
return (MyDTO) this.xmlMarshaller.loadXML(String.format("%s/%s.xml", xmlPath, testID));
}
致:
注意:这不是任何抽象的解决方案,只是使用ConfigurableObjectInputStream类的提示。在启用STS插件的eclipse中运行项目时,我遇到了同样的问题。即使我从项目中完全删除了devtools依赖项。它仍然在eclipse中启用。要解决这个问题,我必须禁用devtools
您的类路径上是否有相同的
类两次?或者您是否从多个类加载器(或WebApp环境中的示例)加载了它。导致类无法强制转换为自身问题的常见原因是类从不同的位置加载…据猜测,这看起来像是某种类加载程序问题。看起来您有两个不同的类加载器,它们加载了相同的类。@sisyphus我使用spring boot+devtools。我读过一些文章,其中devtools保存了一个用于静态jar的类加载器和一个用于应用程序代码的类加载器。这会导致问题吗?@Boristeider是的,就是这样,我删除了spring boot开发工具及其工作原理。谢谢,伙计,我应该提出一个关于弹簧靴的问题。根据他们的指导方针,他们正在观察这个标签。我也不认为这是可以解决的,请建议。
@Cacheable("tests")
public MyDTO loadData(String testID) {
// add file extension to match XML file
return (MyDTO) this.xmlMarshaller.loadXML(String.format("%s/%s.xml", xmlPath, testID));
}
@Cacheable("tests")
public MyDTO loadData(String testID) {
// add file extension to match XML file
Object dtoObject = this.xmlMarshaller.loadXML(String.format("%s/%s.xml", xmlPath, testID));
byte[] data = serializeDTO(dtoObject);
MyDTO dto = deserializeDTO(data);
return dto;
}
private MyDTO deserializeDTO(byte[] data) {
MyDTO dto = null;
try {
ByteArrayInputStream fileIn = new ByteArrayInputStream(data);
ObjectInputStream in = new ConfigurableObjectInputStream(fileIn,
Thread.currentThread().getContextClassLoader());
dto = (MyDTO) in.readObject();
in.close();
fileIn.close();
} catch (Exception e) {
String msg = "Deserialization of marshalled XML failed!";
LOG.error(msg, e);
throw new RuntimeException(msg, e);
}
return dto;
}
private byte[] serializeDTO(Object dtoObject) {
byte[] result = null;
try {
ByteArrayOutputStream data = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(data);
out.writeObject(dtoObject);
out.close();
result = data.toByteArray();
data.close();
} catch (IOException e) {
String msg = "Serialization of marshalled XML failed!";
LOG.error(msg, e);
throw new RuntimeException(msg, e);
}
return result;
}