java – Spring 3.1.1 MVC @Cacheable没有被击中

我正在编写一个webapp,并尝试缓存从控制器调用的一些pojos.

当我尝试使用org.springframework.cache.annotation.Cacheable时,我无法获得任何工作,所以我切换到com.googlecode.ehcaceh.annotations.Cacheable,我仍然无法将其缓存.

我的代码看起来像这样:

@Controller
 public class Conttroller {
    @RequestMapping(method = RequestMethod.GET)....
    public Person getPerson(String id) {
        LOG.debug("Trying to get resource...);
        Person person = personService.detect(id);
        LOG.debug("got person");

并且服务看起来像:

public class PersonService {

@Cacheable(cacheName="deviceCache") 
public Person detect(String id) {
LOG.debug("cache missed")

我的applicationContext看起来像

factorybean" p:configLocation="/meta-inf/ehcache.xml" />

我的ehcache看起来像:

disk="false" diskPersistent="false" timetoIdleSeconds="0"
    timetoLiveSeconds="600" memoryStoreevictionPolicy="LRU"/>

disk="false" diskPersistent="false"
    timetoIdleSeconds="0" timetoLiveSeconds="300"
    memoryStoreevictionPolicy="LRU" />

有趣的是它在我的单元测试中缓存…

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:meta-inf/applicationContext-test.xml"})
public class PersonServiceTest {

private static final Logger LOG = LoggerFactory.getLogger(PersonServiceTest.class);
@Test
public void testCache() {
    LOG.debug("Getting person...");
    Device detect = personService.detect("test");
    LOG.debug("Getting person again ...");
    detect = personService.detect("test");
    LOG.debug("Done");
}

产生:

    13:45:02.885 [main] DEBUG PersonServiceTest - Getting person...
13:45:02.887 [main] DEBUG c.g.e.a.i.CacheAttributeSourceImpl - Adding CACHE advised method 'detect' with attribute: CacheableAttributeImpl [cacheInstanceResolver=com.googlecode.ehcache.annotations.resolver.SingletonCacheableCacheResolver@5c2bfdff,cacheKeyGenerator=HashCodeCacheKeyGenerator [includeMethod=true,includeParameterTypes=true,useReflection=false,checkforCycles=false],parameterMask=ParameterMask [mask=[]]]
13:45:02.889 [main] DEBUG c.g.e.a.i.EhCacheInterceptor - Generated key '-1043428721379252' for invocation: ReflectiveMethodInvocation: public abstract com.mycompany.Person com.mycompany.PersonServiceImpl.detect(java.lang.String); target is of class [com..mycompany.PersonServiceImpl]
13:45:02.889 [main] DEBUG PersonServiceTest - Missed 
13:45:02.927 [main] DEBUG PersonServiceTest  - Getting person again ...
13:45:02.927 [main] DEBUG c.g.e.a.i.EhCacheInterceptor - Generated key '-1043428721379252' for invocation: ReflectiveMethodInvocation:
13:45:02.927 [main] DEBUG PersonServiceTest  - Done

但我的战争输出(通过tomcat7 / eclipse wtp运行)是:

   DEBUG net.sf.ehcache.config.ConfigurationFactory Configuring ehcache from InputStream
DEBUG net.sf.ehcache.config.BeanHandler Ignoring ehcache attribute xmlns:xsi
DEBUG net.sf.ehcache.config.BeanHandler Ignoring ehcache attribute xsi:noNamespaceSchemaLocation
DEBUG net.sf.ehcache.util.PropertyUtil propertiesstring is null.
DEBUG net.sf.ehcache.config.ConfigurationHelper No CacheManagerEventListenerFactory class specified. Skipping...
DEBUG net.sf.ehcache.Cache No BootstrapCacheLoaderFactory class specified. Skipping...
DEBUG net.sf.ehcache.Cache CacheWriter factory not configured. Skipping...
DEBUG net.sf.ehcache.config.ConfigurationHelper No CacheExceptionHandlerFactory class specified. Skipping...
DEBUG net.sf.ehcache.Cache No BootstrapCacheLoaderFactory class specified. Skipping...
DEBUG net.sf.ehcache.Cache CacheWriter factory not configured. Skipping...
DEBUG net.sf.ehcache.config.ConfigurationHelper No CacheExceptionHandlerFactory class specified. Skipping...
DEBUG net.sf.ehcache.store.MemoryStore Initialized net.sf.ehcache.store.NotifyingMemoryStore for deviceCache
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Pass-Through Statistic: LOCAL_OFfheAP_SIZE
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Pass-Through Statistic: LOCAL_OFfheAP_SIZE_BYTES
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Pass-Through Statistic: LOCAL_disK_SIZE
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Pass-Through Statistic: LOCAL_disK_SIZE_BYTES
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Pass-Through Statistic: WRITER_QUEUE_LENGTH
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Pass-Through Statistic: REMOTE_SIZE
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: OFfheAP_GET
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: OFfheAP_PUT
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: OFfheAP_REMOVE
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: disK_GET
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: disK_PUT
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: disK_REMOVE
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: XA_COMMIT
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: XA_ROLLBACK
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: XA_RECOVERY
DEBUG net.sf.ehcache.Cache Initialised cache: deviceCache
DEBUG net.sf.ehcache.config.ConfigurationHelper CacheDecoratorFactory not configured. Skipping for 'personCache'.
DEBUG net.sf.ehcache.config.ConfigurationHelper CacheDecoratorFactory not configured for defaultCache. Skipping for 'personCache'.
DEBUG com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl Adding CACHE advised method 'detect' with attribute: CacheableAttributeImpl [cacheInstanceResolver=com.googlecode.ehcache.annotations.resolver.SingletonCacheableCacheResolver@7a1b0c08,parameterMask=ParameterMask [mask=[]]]
Apr 3,2013 2:03:12 PM org.springframework.web.context.ContextLoader initWebApplicationContext
DEBUG com.mycompany.Controller trying to get resource'
DEBUG com.mycompany.PersonServiceImpl Missed 
DEBUG com.mycompany.Controller got person'
DEBUG com.mycompany.Controller trying to get resource'
DEBUG com.mycompany.PersonServiceImpl Missed 
DEBUG com.mycompany.Controller got person'

所以我的问题是,为什么它在我的单元测试中起作用,而不是webapp?我将如何使用spring注释而不是googlecode注释?

最佳答案
您的PersonService服务未实现接口. Ehcache-spring-annotations需要一个接口,如FAQ中所述:

Requirement 1: your class MUST
implement some (any) interface. If your class does not implement an
interface,this project will not be able to create a Proxy to mimic
your class and apply the cache semantics around your annotated
methods.

相关文章

这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...