fix: CacheConfig bean 返回类型改为 CustomRedisCacheManager 修复冷启动注入失败#123
Open
JasonYv wants to merge 1 commit into
Open
fix: CacheConfig bean 返回类型改为 CustomRedisCacheManager 修复冷启动注入失败#123JasonYv wants to merge 1 commit into
JasonYv wants to merge 1 commit into
Conversation
原 @bean 方法返回 CacheManager 接口,冷启动时 Spring 走静态类型分析, 按 RedisCacheManager 类型注入会失败(IDE 热加载因按实际对象类型注册 能命中,所以问题被掩盖)。改为返回 CustomRedisCacheManager,冷热启动 均可正常注入。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
sa-base的CacheConfig#cacheManager()用@Bean声明缓存管理器时,返回类型写的是接口CacheManager,但实际返回的是CustomRedisCacheManager(RedisCacheManager的子类)。问题
业务代码中按
RedisCacheManager类型注入该 bean 时:RedisCacheManager能命中,所以问题被掩盖,长期没暴露。@Bean返回类型是CacheManager,就拒绝匹配RedisCacheManager,启动直接失败。这是一个潜伏问题,只在冷启动时才会被引爆。
修复
把
@Bean方法的返回类型从CacheManager改成CustomRedisCacheManager,让 Spring 在静态分析阶段就识别该 bean 是RedisCacheManager的子类,冷热启动都能正常注入。改动文件
smart-admin-api-java17-springboot3/sa-base/.../config/CacheConfig.javasmart-admin-api-java8-springboot2/sa-base/.../config/CacheConfig.java顺手删除了不再使用的
import org.springframework.cache.CacheManager;。影响范围
仅修改 bean 声明的返回类型,运行时行为完全一致;不影响现有按
CacheManager类型注入的代码。