如何将 Short 数据类型与 @Cacheable 一起使用?
How to use Short datatype with @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]
这是因为 cacheManager
在 AbstractCacheResolver
中是 null
而抛出的。但是为什么?
好吧,乍一看,您的配置(大部分)看起来是正确的。但是,它实际上也没有告诉我们整个故事,即您的应用程序在什么运行时上下文中 运行.
您似乎在使用 Spring Boot (?) 鉴于您使用 @EnableAutoConfiguration
。你说你有 @Configuration
注释但没有提供任何证据。也许,与流行的看法相反,@AutoConfiguration
does not declare @Configuration
for you; @SpringBootApplication
annotation does (specifically, 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 的 auto配置支持自动检测类路径上的缓存提供程序,然后只需在 @Service
应用程序组件上使用 @Cacheable
注释以及配置中的 @EnableCaching
注释,这就足够了 Spring 启动 为您(自动)配置一个CacheManager
。
因此,下面的@Bean
定义是没有必要的...
@Bean
ConcurrentMapCacheManager cacheManager() {
return new ConcurrentMapCacheManager();
}
有关详细信息,请参阅 Spring Boot's Reference Guide。
我正在尝试使用 @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]
这是因为 cacheManager
在 AbstractCacheResolver
中是 null
而抛出的。但是为什么?
好吧,乍一看,您的配置(大部分)看起来是正确的。但是,它实际上也没有告诉我们整个故事,即您的应用程序在什么运行时上下文中 运行.
您似乎在使用 Spring Boot (?) 鉴于您使用 @EnableAutoConfiguration
。你说你有 @Configuration
注释但没有提供任何证据。也许,与流行的看法相反,@AutoConfiguration
does not declare @Configuration
for you; @SpringBootApplication
annotation does (specifically, 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 的 auto配置支持自动检测类路径上的缓存提供程序,然后只需在 @Service
应用程序组件上使用 @Cacheable
注释以及配置中的 @EnableCaching
注释,这就足够了 Spring 启动 为您(自动)配置一个CacheManager
。
因此,下面的@Bean
定义是没有必要的...
@Bean
ConcurrentMapCacheManager cacheManager() {
return new ConcurrentMapCacheManager();
}
有关详细信息,请参阅 Spring Boot's Reference Guide。