gpt4 book ai didi

java - 如何将 Short 数据类型与 @Cacheable 一起使用?

转载 作者:行者123 更新时间:2023-12-01 09:44:11 25 4
gpt4 key购买 nike

我正在尝试使用 @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]

抛出此错误是因为 AbstractCacheResolver 中的 cacheManagernull。但为什么呢?

最佳答案

乍一看,您的配置看起来(大部分)是正确的。然而,它实际上也没有告诉我们整个故事,即您的应用程序在什么运行时上下文中运行。

鉴于您使用了@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了解更多详情。

关于java - 如何将 Short 数据类型与 @Cacheable 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38202997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com