Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring 3.1.1 MVC@Cacheable未被命中_Java_Spring_Spring Mvc_Ehcache - Fatal编程技术网

Java Spring 3.1.1 MVC@Cacheable未被命中

Java Spring 3.1.1 MVC@Cacheable未被命中,java,spring,spring-mvc,ehcache,Java,Spring,Spring Mvc,Ehcache,我正在编写一个webapp,并试图缓存一些从控制器调用的pojo 当我尝试使用org.springframework.cache.annotation.Cacheable时,我无法让任何东西正常工作,所以我切换到com.googlecode.ehcaceh.annotations.Cacheable,但仍然无法将其缓存 我的代码如下所示: @Controller public class Conttroller { @RequestMapping(method = RequestMet

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

当我尝试使用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")
<defaultCache eternal="false" maxElementsInMemory="1000"
    overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
    timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/>

<cache name="deviceCache"
    maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false"
    timeToIdleSeconds="0" timeToLiveSeconds="300"
    memoryStoreEvictionPolicy="LRU" />
我的应用程序上下文看起来像

<context:component-scan base-package="com.mycompany" />


         <ehcache:annotation-driven cache-manager="ehCacheManager" />
         <bean id="cacheManager"      class="org.springframework.cache.ehcache.EhCacheCacheManager">
            <property name="cacheManager">
                <ref bean="ehCacheManager" />
            </property>
        </bean>
        <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="/META-INF/ehcache.xml" />
产生:

    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
但是我的war(通过tomcat7/eclipse wtp运行)的输出是:


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

您的
PersonService
服务没有实现接口。Ehcache spring注释需要一个接口,如中所述:

要求1:你的班级必须 实现一些(任何)接口。如果您的类没有实现 接口,此项目将无法创建代理来模拟 您的类并在注释的对象周围应用缓存语义 方法


尽管我有一个接口,但我在实现类上有@Cacheable注释。一旦我将它移动到接口类,它就被缓存了。

您需要在spring上下文xml或spring配置文件中启用eh cache注释。比如说

<ehcache:annotation-driven cache-manager="ehCacheManager" />


即使它实现了一个接口,我也有同样的问题自从我使用它已经有一段时间了,但我模糊地记得必须在我的一个缓存的no args方法中添加一个伪方法参数,以便Ehcache可以将缓存的返回值与该方法匹配。值得一试。。。。
   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, cacheKeyGenerator=HashCodeCacheKeyGenerator [includeMethod=true, includeParameterTypes=true, useReflection=false, checkforCycles=false], 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'
<ehcache:annotation-driven cache-manager="ehCacheManager" />