如何在@Cacheable中使用Short数据类型?

时间:2016-07-05 12:04:00

标签: java spring caching spring-cache

我正在尝试使用Short缓存@Cacheable值。

@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]

这是因为cacheManager null AbstractCacheResolver。但为什么呢?

1 个答案:

答案 0 :(得分:1)

嗯,乍一看,您的配置看起来(大多数)是正确的。但是,它实际上并没有告诉我们整个故事,即在应用程序运行的运行时上下文中。

在您使用@EnableAutoConfiguration的情况下,您似乎正在使用 Spring Boot (?)。你说你有@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注释, Spring Boot 自动配置支持会自动检测缓存类路径上的提供程序,然后只需在@Cacheable应用程序组件上使用@Service注释以及配置中的@EnableCaching注释,这对于 Spring Boot 到(自动)为您配置CacheManager

因此,以下@Bean定义不是必需的......

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

有关详细信息,请参阅Spring Boot's Reference Guide