java - 无法使用 YAML Spring Profile 禁用 @Cacheable

标签 java spring spring-boot ehcache spring-cache

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

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

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

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

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

在我的类里面,我有一个字段向我显示它是“dev”配置文件:

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

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

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

最佳答案

您可以通过配置文件派生您的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 .

关于java - 无法使用 YAML Spring Profile 禁用 @Cacheable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46817834/

相关文章:

java - 使用 AngularJS 和 Spring Boot 的 Websocket

java - 将递归方法更改为迭代方法

java - Io异常: The Network Adapter could not establish the connection

java - 一段时间后彻底杀死 future

java - 如何在 Angular 应用程序上显示来自 springboot 的异常消息

java - Oauth 保护的休息 API 无需持有者 token 即可工作

java - 如何从多模块 Maven 项目中组装多个可执行 JAR

java - Spring Boot 应用程序无法 Autowiring XML 配置

java - quartz 调度程序中的作业被多次调用

spring-boot - kafka嵌入: java. io.FileNotFoundException :/tmp/kafka-7785736914220873149/replication-offset-checkpoint. tmp