spring-boot-devtools causing ClassCastException while getting from cache.

霸气de小男生 提交于 2019-11-30 14:04:11

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:

  1. Disable cache when you're running your application in development
  2. Use a different cache manager (if you're using Spring Boot 1.3, you could force a simple cache manager using the spring.cache.type property in application-dev.properties and enable the dev profile in your IDE)
  3. 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

Well I got the same error, but the caching was not the reason. Actually I was using caching, but the commenting the caching out didn't help.

Based on the hints here and there I just introduced additional serialization/derialization of my object. It's definatelly the best way (the performance issue), but it's working.

So, just for the others I changed my code from:

@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));
}

to:

@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;
}

Note: this is not any sofisticated solution, but just the hint of usage ConfigurableObjectInputStream class.

I was running into this same issue when running a project in eclipse with the STS plugin enabled. Even though I removed the devtools dependency completely from the project. It was still enabled in eclipse. To fix this, I had to disable devtools.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!