0
我正在學習Spring和Data JPA。我對Ehcache有問題。我想緩存從數據庫返回一些記錄的方法之一的返回值。這是Ehcache實例預先配置的練習(我假設)。問題是我不能使用註釋@Cacheable將我的方法標記爲應該緩存其返回值的方法。我得到一個不兼容的類型編譯錯誤(必需:boolean,found:String)。這裏是我的服務層的類,我想我應該把@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 /春/):
<?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>
非常感謝!我對獲得答案感到失望。由於詢問的時間不恰當(這是歐洲的午夜時間),我的問題在沒有看到的情況下沒有得到答覆,我又不能再問了,因爲它會被標記爲重複問題!再次感謝你。但是如果Spring會使用與java原始註釋名稱不同的另一個註釋,那會更好嗎?我使用Netbeans的自動導入功能,並不知道有另一個具有相同名稱的註釋。 – Metallica
如果名稱必須有意義,那麼很難在整個Java生態系統中定義具有唯一名稱的類(註釋)。這就是爲什麼Java類是由完整的類名(包名+類名)標識的原因。關於你的代碼,它是否編譯?根據JEE [文檔](http://docs.oracle.com/javaee/6/api/javax/persistence/Cacheable.html),javax.persistence.Cacheable不能應用於方法。 – ragnor