Spring Boot 3.x集成Caffeine异步缓存报错问题求助
Spring Boot 3.x配置Caffeine异步缓存报错问题
错误信息
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in class path resource [io/beapi/api/config/CacheConfiguration.class]: Failed to instantiate [org.springframework.cache.CacheManager]: Factory method 'cacheManager' threw exception with message: No signature of method: org.springframework.cache.caffeine.CaffeineCacheManager.registerCustomCache() is applicable for argument types: (String, com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalAsyncCache) values: [ApiCache, com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalAsyncCache@43ab0659] Possible solutions: registerCustomCache(java.lang.String, com.github.benmanes.caffeine.cache.Cache) at app//org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:659) at app//org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:493) at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1332) at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1162) at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) at app//org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) at app//org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at app//org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) at app//org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) at app//org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1417) at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) at app//org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:710) ... 122 more Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cache.CacheManager]: Factory method 'cacheManager' threw exception with message: No signature of method: org.springframework.cache.caffeine.CaffeineCacheManager.registerCustomCache() is applicable for argument types: (String, com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalAsyncCache) values: [ApiCache, com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalAsyncCache@43ab0659] Possible solutions: registerCustomCache(java.lang.String, com.github.benmanes.caffeine.cache.Cache) at app//org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:171) at app//org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655) ... 135 more Caused by: groovy.lang.MissingMethodException: No signature of method: org.springframework.cache.caffeine.CaffeineCacheManager.registerCustomCache() is applicable for argument types: (String, com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalAsyncCache) values: [ApiCache, com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalAsyncCache@43ab0659] Possible solutions: registerCustomCache(java.lang.String, com.github.benmanes.caffeine.cache.Cache) at app//io.beapi.api.config.CacheConfiguration.cacheManager(CacheConfiguration.groovy:35) at app//org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:258) at app//org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) at app//org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:139) ... 136 more
配置代码
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import com.github.benmanes.caffeine.cache.CacheLoader import java.io.IOException; import java.util.concurrent.TimeUnit; import java.time.Duration; import com.github.benmanes.caffeine.cache.AsyncCache import com.github.benmanes.caffeine.cache.AsyncLoadingCache; @Configuration @EnableCaching public class CacheConfiguration { @Bean public CacheManager cacheManager() { CaffeineCacheManager manager = new CaffeineCacheManager(); manager.registerCustomCache("ApiCache", Caffeine.newBuilder().maximumSize(200).expireAfterAccess(5, TimeUnit.MINUTES).buildAsync()); manager.registerCustomCache("StatsCache", Caffeine.newBuilder().maximumSize(500).expireAfterAccess(10, TimeUnit.MINUTES).build()); manager.registerCustomCache("Trace", Caffeine.newBuilder().maximumSize(50).expireAfterAccess(10, TimeUnit.MINUTES).build()); return manager; } @Bean(name='sessionService') @ConditionalOnMissingBean public SessionService sessionService() throws IOException { return new SessionService(); } @Bean @ConditionalOnMissingBean public ApiCacheService apiCacheService() throws IOException { return new ApiCacheService(); } @Bean @ConditionalOnMissingBean public TraceCacheService traceCacheService() throws IOException { return new TraceCacheService(cacheManager()); } @Bean @ConditionalOnMissingBean public StatsCacheService statsCacheService() throws IOException { return new StatsCacheService(cacheManager()); } }
问题分析
报错核心是类型不匹配:CaffeineCacheManager.registerCustomCache()方法要求第二个参数是com.github.benmanes.caffeine.cache.Cache(同步缓存)类型,但调用buildAsync()返回的是AsyncCache(异步缓存)类型,两者无法兼容,因此抛出方法签名不匹配错误。
解决方案
方案1:使用Spring缓存抽象的异步注解(推荐)
无需手动创建AsyncCache,通过Spring缓存注解的async=true属性开启异步能力,底层自动利用Caffeine的异步机制:
修改后的缓存配置:
import org.springframework.cache.CacheManager import org.springframework.cache.annotation.EnableCaching import org.springframework.cache.caffeine.CaffeineCacheManager import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import com.github.benmanes.caffeine.cache.Caffeine import java.util.concurrent.TimeUnit @Configuration @EnableCaching class CacheConfiguration { @Bean CacheManager cacheManager() { CaffeineCacheManager manager = new CaffeineCacheManager() // 为ApiCache配置同步缓存规则 manager.registerCustomCache("ApiCache", Caffeine.newBuilder() .maximumSize(200) .expireAfterAccess(5, TimeUnit.MINUTES) .build()) // 使用build()而非buildAsync() manager.registerCustomCache("StatsCache", Caffeine.newBuilder() .maximumSize(500) .expireAfterAccess(10, TimeUnit.MINUTES) .build()) manager.registerCustomCache("Trace", Caffeine.newBuilder() .maximumSize(50) .expireAfterAccess(10, TimeUnit.MINUTES) .build()) return manager } // 其他Bean定义保持不变 }
在需要异步缓存的方法上添加async=true:
@Cacheable(value = "ApiCache", async = true) CompletableFuture<Data> fetchData(String key) { // 异步加载数据逻辑 return CompletableFuture.supplyAsync(() -> { // 实际数据获取操作 return dataService.getData(key); }); }
方案2:直接管理AsyncCache实例(适用于细粒度控制场景)
如果需要直接操作异步缓存,不要通过CaffeineCacheManager.registerCustomCache注册,而是单独创建AsyncCache Bean:
import org.springframework.cache.annotation.EnableCaching import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import com.github.benmanes.caffeine.cache.AsyncCache import com.github.benmanes.caffeine.cache.Caffeine import java.util.concurrent.TimeUnit @Configuration @EnableCaching class CacheConfiguration { @Bean AsyncCache apiAsyncCache() { return Caffeine.newBuilder() .maximumSize(200) .expireAfterAccess(5, TimeUnit.MINUTES) .buildAsync() } // 其他缓存和服务Bean定义保持不变 }
在服务中注入AsyncCache直接使用:
@Service class ApiCacheService { private final AsyncCache apiAsyncCache ApiCacheService(AsyncCache apiAsyncCache) { this.apiAsyncCache = apiAsyncCache } CompletableFuture<Data> getFromCache(String key) { return apiAsyncCache.get(key, k -> dataService.getData(k)) } }
关键注意点
CaffeineCacheManager核心作用是管理同步缓存,不支持直接注册AsyncCache实例。- Spring缓存抽象的
async=true注解已封装异步缓存逻辑,是最简洁的实现方式。 - 直接操作
AsyncCache适合不需要Spring缓存抽象、需要自定义异步缓存逻辑的场景。
内容的提问来源于stack exchange,提问作者Orubel
相关产品推荐
相关产品推荐

