2012-06-25 80 views
2

我已經升級了我的應用程序到Spring 3.1,並且所有的jar已經被充分更新。但是當我嘗試在我的控制器中的某個方法中使用@Cacheable時,該控制器的所有方法的URL映射都會中斷。在檢查日誌文件時,我發現從未檢測到該控制器所有方法的URL映射。我很確定我的緩存配置沒問題。 任何人都可以給我一些線索,因爲我可能做錯了什麼。Spring @Cacheable正在破解@RequestMapping

ehcache.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<ehcache> 
<defaultCache 
eternal="false" 
maxElementsInMemory="2" 
overflowToDisk="false" 
diskPersistent="false" 
timeToLiveSeconds="300" 
memoryStoreEvictionPolicy="LRU" /> 

<cache name="Backlog" 
eternal="false" 
maxElementsInMemory="2" 
overflowToDisk="false" 
diskPersistent="false" 
timeToLiveSeconds="300" 
memoryStoreEvictionPolicy="LRU" /> 
</ehcache> 

配置:

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> 
    <property name="cacheManager"> 
     <ref bean="ehcache" /> 
    </property> 
</bean> 
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="/WEB-INF/spring-configuration/ehcache.xml" /> 

代碼片段:

@RequestMapping("/*/backlog") 
@Cacheable(value = "Backlog") 
public ModelAndView getBackLog(){ 
    //sth here 
} 

感謝您的幫助。

回答

4

方法雖然@mana解釋如何解決這個問題,這就是爲什麼添加@Cacheable傷了你的代碼。最近的blog post更詳細地解釋了這一點,非常值得一讀。

默認情況下,Spring創建JDK動態代理以實現緩存行爲,這要求被代理的類實現了一個接口,該接口聲明瞭您希望在類@Cacheable上公開的所有方法。值得注意的是,如果您將Spring配置爲使用基於CGLIB的代理,則不必實現任何接口。

您沒有提供任何特定的錯誤,但是在這種情況下,您經常會遇到找不到方法的例外情況。 Spring試圖調用代理上的getBackLog()方法,但沒有一個。

+0

非常感謝,它解決了這個問題。 – pareshverma91

+0

@ pareshverma91如何正確地解決這個問題..很好幫助 –

3

您不應該緩存控制器方法本身,而是會調用資源飢餓的方法來創建積壓。看看at this similar question@Cachable所做的是爲您的函數參數和相關的返回值創建鍵值映射。在你的情況下,這將是一個ModelAndView對象。

如果你真的需要服務器端網頁緩存也許使用this Apache Cache Module

+0

只有這個控制器方法調用了很多不同的資源飢餓的其他方法。所以我想緩存這個方法本身。 @Alex答案解決了我的問題。另外我想緩存ModelAndView而不是最終視圖,因爲攔截器需要添加其他組件。謝謝您的幫助。 – pareshverma91

0

你應該注射服務類在控制器和緩存服務類