spring mvc我无法缓存数据¿为什么?

时间:2014-10-12 11:53:49

标签: spring spring-mvc caching

我试图在spring mvc 4.1.1中使用一个简单的例子来处理数据,但是我可以解决,有些人知道为什么吗?

编辑:

现在工作正常,谢谢

加载了servlet上下文

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"             xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<cache:annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory -->
<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.leanupp.researcher" />

<beans:bean id="cacheManager"
    class="org.springframework.cache.support.SimpleCacheManager">
    <beans:property name="caches">
        <beans:set>
            <beans:bean
                class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                <beans:property name="name" value="myData" />
            </beans:bean>
        </beans:set>
    </beans:property>
</beans:bean>

<beans:bean id="parserService" class="com.leanupp.researcher.ParserServiceImpl"/> // that par is      //complete necesary

控制器:     //编辑     @Autowired     ParserService parseService;

@RequestMapping(value="/parse", method=RequestMethod.GET)
public String parse(Model model){

    List<Data> dataList=new ParserServiceImpl().parse();
    model.addAttribute("dataList",dataList);
    return "parse";
}

解析器服务接口:

public interface ParserService {

// @Cacheable(value="myData") goes to implemented concrete class
public List<Data> parse();
}

和解析器实现:

public class ParserServiceImpl implements ParserService {



private static final Logger logger = LoggerFactory
        .getLogger(HomeController.class);

public ParserServiceImpl() {

}

  @Override
  @Cacheable(value="myData") 
  public List<Data> parse() {

    List<Data> dataList = new ArrayList<Data>();

        Data data= (Data) new Finder().getData(0);

        dataList.add(data);

        logger.info("data is not cached");

    }

    return dataList;

}

1 个答案:

答案 0 :(得分:-1)

我认为你需要为你的数据(POJO)实现可序列化的类。而且我不知道为什么你没有cache.xml。请参阅此https://mytechrepo.wordpress.com/2015/07/23/caching-data-with-spring-3-1-1-and-ehcache-demo/