春天@Cacheable打破了@RequestMapping [英] Spring @Cacheable is breaking @RequestMapping

查看:150
本文介绍了春天@Cacheable打破了@RequestMapping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将我的应用程序升级到了Spring 3.1,并且所有jars都进行了充分的更新。但是,当我尝试对一个控制器中的方法使用@Cacheable时,该控制器的所有方法的URL映射都会中断。在检查日志文件时,我发现从未检测到该控制器所有方法的URL映射。我很确定我的缓存配置很好。
谁能给我一些我可能做错了什么的线索。

I have upgraded my application to Spring 3.1 and all the jars have been adequately updated. But when I try to use @Cacheable for a method in one of my controllers, URL mapping for all the methods of that controller breaks. On checking the log files I found that the URL mapping for all the methods of that controller were never detected. I am pretty sure that my cache configurations are fine. Can anyone give me some clue as what I might be doing wrong.

ehcache.xml

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
}

感谢您的帮助。

推荐答案

虽然@mana解释了解决方法,但这就是为什么添加 @Cacheable 会破坏您的代码的原因。最近的博客文章对此进行了更多解释

Whilst @mana has explained how to fix this, this is why adding @Cacheable breaks your code. A recent blog post explains this in more detail and is well worth a read.

默认情况下,Spring创建JDK动态代理来实现缓存行为,这要求被代理的类实现一个接口,该接口声明所有您希望在 @Cacheable 类中公开的方法。值得注意的是,如果将Spring配置为使用基于CGLIB的代理,则无需实现任何接口。

By default Spring creates JDK dynamic proxies to achieve the caching behaviour, this requires that the class being proxied implements an interface which declares all the methods you wish to expose on your @Cacheable class. It is worth noting that you don't have to implement any interfaces if you configure Spring to use CGLIB based proxies.

在这种情况下,您没有提供任何特定的错误,但通常会得到未发现异常的方法。 Spring尝试在代理上调用 getBackLog()方法,但没有一个方法。

You haven't supplied any specific errors but often you get a method not found exception in this scenario. Spring tries to invoke the getBackLog() method on the proxy and there isn't one.

这篇关于春天@Cacheable打破了@RequestMapping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆