2016-07-05 85 views
0

我試圖緩存Short值與@Cacheable如何在@Cacheable中使用Short數據類型?

@EnableAutoConfiguration 
@EnableCaching 
public class Config { 
    @Bean 
    public CacheManager cacheManager() { 
     return new ConcurrentMapCacheManager(); 
    } 
} 

@Service 
public class MyCacheService { 
    @Cacheable("testcache") 
    public Short getId(String name) { 
     return 1; 
    } 
} 

結果:

java.lang.NullPointerException 
    at org.springframework.cache.interceptor.AbstractCacheResolver.resolveCaches(AbstractCacheResolver.java:78) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.cache.interceptor.CacheAspectSupport.getCaches(CacheAspectSupport.java:216) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.<init>(CacheAspectSupport.java:565) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.cache.interceptor.CacheAspectSupport.getOperationContext(CacheAspectSupport.java:229) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContexts.<init>(CacheAspectSupport.java:508) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:302) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) ~[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE] 

這是因爲拋出是cacheManagerAbstractCacheResolvernull。但爲什麼?

+0

你有@Configuration註釋嗎? – Jango

+0

是或我擁有它。 – membersound

回答

1

那麼,乍一看,你的配置看起來(大部分)是正確的。但是,它並沒有告訴我們整個故事,也就是說,在您的應用程序正在運行的運行環境中。

您似乎正在使用Spring Boot(?)給出您使用@EnableAutoConfiguration。你說你有@Configuration註釋但還沒有提供任何證據。也許,與流行的看法相反,@AutoConfiguration並未爲您申報@Configuration; @SpringBootApplication註釋確實(具體地說,here)。

無論如何,我放了一個簡單的Spring Boot應用程序來演示您的示例代碼。它按預期工作。

package org.examples.spring.boot.caching; 

import static org.assertj.core.api.Assertions.assertThat; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.CommandLineRunner; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cache.annotation.Cacheable; 
import org.springframework.cache.annotation.EnableCaching; 
import org.springframework.cache.concurrent.ConcurrentMapCacheManager; 
import org.springframework.context.annotation.Bean; 
import org.springframework.stereotype.Service; 

@SpringBootApplication 
@EnableCaching 
public class ConcurrentMapCachingExample implements CommandLineRunner { 

    public static void main(String[] args) { 
    SpringApplication.run(ConcurrentMapCachingExample.class, args); 
    } 

    @Autowired 
    private ExampleCacheableService exampleService; 

    @Override 
    public void run(String... args) throws Exception { 
    assertThat(exampleService.isCacheMiss()).isFalse(); 
    assertThat(exampleService.computeValue("one").intValue()).isEqualTo(1); 
    assertThat(exampleService.isCacheMiss()).isTrue(); 
    assertThat(exampleService.computeValue("one").intValue()).isEqualTo(1); 
    assertThat(exampleService.isCacheMiss()).isFalse(); 
    } 
} 

@Service 
class ExampleCacheableService { 

    boolean cacheMiss; 

    boolean isCacheMiss() { 
    boolean cacheMiss = this.cacheMiss; 
    this.cacheMiss = false; 
    return cacheMiss; 
    } 

    @Cacheable("Example") 
    public Short computeValue(String cacheKey) { 
    System.out.printf("Computing value for [%s]%n", cacheKey); 
    cacheMiss = true; 
    return 1; 
    } 
} 

另外要注意,考慮到@SpringBootApplication註釋declares@EnableAutoConfiguration標註爲你,那春天啓動的自動配置支持,只需使用您的@Service應用程序組件的@Cacheable註釋自動檢測在classpath緩存提供者,然後以及配置中的@EnableCaching註釋,這足以讓Spring Boot爲您自動配置CacheManager

因此,下面的@Bean定義是沒有必要的......

@Bean 
ConcurrentMapCacheManager cacheManager() { 
    return new ConcurrentMapCacheManager(); 
} 

有關詳細信息,請參閱Spring Boot's Reference Guide

+0

感謝您的詳細見解。我不知道爲什麼在我的情況下,它不工作。但是你的解釋證明了一般情況下代碼應該沒有問題。 – membersound

相關問題