2013-04-03 34 views
5

Tôi đang viết một ứng dụng web và cố gắng lưu vào bộ nhớ cache một số pojos được gọi từ bộ điều khiển.Mùa xuân 3.1.1 MVC @ Có thể không được nhấn

Tôi không thể nhận được bất kỳ thứ gì hoạt động khi tôi thử sử dụng org.springframework.cache.annotation.Cacheable, vì vậy tôi đã chuyển sang com.googlecode.ehcaceh.annotations.Cacheable và tôi vẫn không thể truy cập vào bộ nhớ cache.

Mã của tôi trông như thế này:

@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"); 

và dịch vụ trông giống như:

public class PersonService { 

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

My applicationContext trông giống như

<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" /> 

và ehcache của tôi trông giống như:

<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" /> 

là gì thú vị là nó lưu trữ tốt trong bài kiểm tra đơn vị của tôi ...

@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"); 
} 

trong đó sản xuất:

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.googleco[email protected]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 

Nhưng đầu ra của chiến tranh của tôi (chạy qua tomcat7/eclipse wtp) là:

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.googleco[email protected]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' 

Vì vậy, câu hỏi của tôi là, tại sao nó hoạt động trong thử nghiệm đơn vị của tôi chứ không phải webapp? Và tôi sẽ sử dụng chú thích mùa xuân thay vì chú thích googlecode như thế nào?

Trả lời

3

Mặc dù tôi đã có giao diện, tôi đã có chú thích @Cacheable trên lớp triển khai. Khi tôi chuyển nó sang lớp giao diện, nó đã được lưu trữ.

10

Dịch vụ PersonService của bạn không triển khai giao diện. Ehcache xuân-chú thích đòi hỏi một giao diện, như mô tả trong FAQ:

Yêu cầu 1: lớp học của bạn PHẢI thực hiện một số (có) giao diện. Nếu lớp học của bạn không triển khai giao diện , dự án này sẽ không thể tạo Proxy để bắt chước lớp học của bạn và áp dụng ngữ nghĩa bộ nhớ cache xung quanh các phương thức có chú thích của bạn.

+0

Tôi có cùng một vấn đề ngay cả với nó thực hiện một giao diện –

+0

là được một thời gian kể từ khi tôi sử dụng nó nhưng tôi mơ hồ nhớ phải thêm một đối số phương pháp giả để một trong cache phương pháp không-args của tôi để Ehcache có thể kết hợp giá trị trả về được lưu trong bộ nhớ cache với phương thức đó. Đáng thử .... – nickdos

0

bạn cần bật chú thích bộ nhớ cache eh trong ngữ cảnh mùa xuân xml hoặc tệp cấu hình Spring. ví dụ

<ehcache:annotation-driven cache-manager="ehCacheManager" /> 
Các vấn đề liên quan