【问题标题】:Unable to disable @Cacheable with YAML Spring Profile无法使用 YAML Spring Profile 禁用 @Cacheable
【发布时间】:2018-03-30 18:42:41
【问题描述】:

我创建了一个名为“mycache”的缓存,它应用于我的服务中的一个方法,例如:

@Cacheable(value = "mycache")
public String getValue(String something) {
   ...breakpoint here...
}

我的application.yml 中也有以下内容:

---
spring:
  profiles: dev
  cache:
    type: NONE

(注意:我也尝试过:无 - 全部小写 - 这也不起作用)

在我的课堂上,我有一个字段显示它“开发”配置文件:

@Value("${spring.profiles.active}")
private String activeProfiles;

当我第一次调用它时(在调试模式下),它会到达断点,但之后每次都不会(这意味着它没有应用 YAML 设置)。

如何通过配置文件禁用它?

【问题讨论】:

  • 您是否检查过您的env 端点以验证类型在运行时设置为none 并且没有其他任何事情再次启用缓存?
  • @DarrenForsythe 我该怎么做?
  • env 端点是 Spring Boot Actuator 的一部分。 docs.spring.io/spring-boot/docs/current/reference/html/… 请注意,如果您使用的是 1.5.0 或更高版本,则默认情况下端点是敏感的 - 禁用 manage.security.disable = false。当自动配置报告设置为none 并且没有通过debug = true 时,可能还想检查自动配置报告
  • github.com/Flaw101/default-boot-app/tree/caching quiok 上面的示例,请参阅控制台日志,说明 CacheConfiguration(s) 由于缓存类型设置为无而未启用。

标签: java spring spring-boot ehcache spring-cache


【解决方案1】:

您可以通过配置文件导出您的CacheManager,如下所示:

@Configuration
@EnableCaching
public class CachingConfig {
 
@Bean
@Profile("test")
public CacheManager cacheManager() {
    SimpleCacheManager cacheManager = new SimpleCacheManager();
    cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("sampleCache")));
    return cacheManager;
}


@Bean
@Profile("test")
public CacheManager noOpcacheManager() {
final CacheManager   cacheManager = new NoOpCacheManager();
 
return cacheManager;
}
}

如果您想对 bean 定义有条件,请参考here

【讨论】:

  • 虽然这通常很有帮助,但它似乎并没有解决人们关于无法通过 YAML 配置禁用它的问题。
  • 使用条件我们可以控制bean,答案更新
猜你喜欢
  • 2015-11-26
  • 1970-01-01
  • 2019-12-02
  • 1970-01-01
  • 2021-01-09
  • 2015-09-04
  • 1970-01-01
  • 2019-03-23
  • 2014-02-24
相关资源
最近更新 更多