fix(cache): 缺 StringRedisTemplate Bean 时自动装配优雅退场
原仅 @ConditionalOnClass(类在 classpath 恒真),StringRedisTemplate Bean 缺失 (多 ConnectionFactory 致 @ConditionalOnSingleCandidate 不满足,或 exclude RedisAutoConfiguration)时会因构造参数缺依赖而启动失败。加 @ConditionalOnBean(StringRedisTemplate),配合既有 @AutoConfigureAfter(RedisAutoConfiguration) 在 Redis 装配后求值,缺 Bean 时整类 back off 而非报错。仅改 cache,不动 stream(接受两者风格暂不一致)。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import com.njcn.middle.cache.support.CacheKeyBuilder;
|
||||
import com.njcn.middle.cache.support.CacheValueCodec;
|
||||
import com.njcn.middle.cache.template.RedisCacheEnhanceTemplate;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
||||
@@ -18,6 +19,7 @@ import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(StringRedisTemplate.class)
|
||||
@ConditionalOnBean(StringRedisTemplate.class)
|
||||
@AutoConfigureAfter(RedisAutoConfiguration.class)
|
||||
@EnableConfigurationProperties(RedisCacheProperties.class)
|
||||
public class RedisCacheAutoConfiguration {
|
||||
|
||||
@@ -40,6 +40,17 @@ class RedisCacheAutoConfigurationTest {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void backsOffWhenNoStringRedisTemplateBean() {
|
||||
// 不装配 RedisAutoConfiguration,上下文内无 StringRedisTemplate Bean:
|
||||
// 应整类 back off 优雅退场,而非因缺依赖启动失败。
|
||||
new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(RedisCacheAutoConfiguration.class))
|
||||
.run(ctx -> assertThat(ctx)
|
||||
.hasNotFailed()
|
||||
.doesNotHaveBean(RedisCacheEnhanceTemplate.class));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class CustomConfig {
|
||||
@Bean
|
||||
|
||||
Reference in New Issue
Block a user