0

我正在学习 Spring 和 Data JPA。我对 Ehcache 有疑问。我想缓存从数据库返回一些记录的方法之一的返回值。这是一个预先配置了 Ehcache 实例的练习(我假设)。问题是我不能使用注解@Cacheable 将我的方法标记为应该缓存其返回值的方法。我收到不兼容的类型编译错误(必需:布尔值,找到:字符串)。这是我认为应该将@Cacheable 放在这里的服务层中的一个类(对吗?):

package wad.datatables.service;

import javax.persistence.Cacheable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import wad.datatables.domain.Book;
import wad.datatables.repository.BookRepository;
import wad.datatables.view.DataTablesResponse;

@Service
public class JpaDataTablesBookService implements DataTablesBookService {

    @Autowired
    private BookRepository bookRepository;    

    @Override
    @Transactional(readOnly = true) 
    @Cacheable("books")
    public DataTablesResponse getBooks(String queryString) {
        Pageable pageable = new PageRequest(0, 10, Sort.Direction.ASC, "title");

        Page<Book> page = bookRepository.findByTitleContaining(queryString, pageable);

        DataTablesResponse response = new DataTablesResponse();
        response.setTotalRecords(page.getTotalElements());
        response.setTotalDisplayRecords(page.getNumberOfElements());
        response.setData(page.getContent());

        return response;
    }
}

还有我的存储库层(只有一个类):

package wad.datatables.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import wad.datatables.domain.Book;

public interface BookRepository extends JpaRepository<Book, Long> {        
    Page<Book> findByTitleContaining(String title, Pageable pageable);
}

这是我的配置文件:

cache.xml(位于WEB-INF/spring/):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="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">

    <cache:annotation-driven cache-manager="cacheManager" />

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcache"/>
    </bean>

    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml" />
    </bean>
</beans>

以及 ehcache.xml(位于 src/main/resources 中):

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:noNamespaceSchemaLocation="ehcache.xsd" 
         updateCheck="true" 
         monitoring="autodetect" 
         dynamicConfig="true">
    <cache name="books" maxEntriesLocalHeap="1000" eternal="true" memoryStoreEvictionPolicy="LRU"/>
</ehcache>
4

1 回答 1

5

该错误是因为您使用了错误的 Cacheable 注释。而不是javax.persistence.Cacheable使用org.springframework.cache.annotation.Cacheable.

于 2013-10-08T05:03:26.463 回答