You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot中@Cacheable与@Bean搭配使用的有效性疑问

在@Configuration类中同时使用@Cacheable与@Bean的问题解析

示例代码

@Configuration
public class CacheManager {

  @Cacheable("urls")
  @Bean
  public Map<String,String> loadUrl(){
   // hashmap population logic
   return urlMap;
  }

}

问题

上述写法会在配置阶段加载所有缓存吗?还是@Cacheable注解会被忽略?

结论与原因

这种写法里@Cacheable注解会被忽略,无法生效。原因是Spring在配置阶段创建Bean时,缓存代理尚未完成初始化并应用,所以@Cacheable定义的缓存逻辑不会被触发。

内容的提问来源于stack exchange,提问作者Ravindu Kottahachchi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 10:22:33