可缓存注释会抛出类强制转换异常

时间:2014-04-24 14:53:31

标签: spring caching arraylist annotations hazelcast

我使用Spring的Cacheable注释来缓存MyArrayList类型的方法的结果:

@Cacheable(value = "list-cache", key = "'list.' + #name")
public MyArrayList<ListItem> getListItems(String name) 
{
}

类MyArrayList是ArrayList的子类:

public class MyArrayList<E> extends ArrayList<E> 
{
}

当使用缓存检索对象时,抛出了类强制转换异常:

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: java.util.ArrayList cannot be cast to pub.MyArrayList
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

java.lang.ClassCastException: java.util.ArrayList cannot be cast to pub.MyArrayList
    pub.controller.HomeController.getListItems(HomeController.java:28)
    pub.controller.HomeController.test(HomeController.java:21)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

我使用hazelcast作为缓存管理器。当我检查Map时,我可以观察到值存储为ArrayList,后来无法转换为正确的类型。

是否可以使用Spring注释和hazelcast缓存管理器存储扩展ArrayList的类?

0 个答案:

没有答案