无法使用YAML Spring Profile禁用@Cacheable [英] Unable to disable @Cacheable with YAML Spring Profile

查看:195
本文介绍了无法使用YAML Spring Profile禁用@Cacheable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a cache called "mycache" which is applied to a method in my Service like:

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

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

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

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

(NOTE: I also tried: none - all lowercase - and that also did not work)

在我的课堂上,我有一个字段向我显示 dev配置文件:

And in my class I have a field that shows me it is the "dev" profile:

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

当我第一次调用它(在调试模式下)时,它会到达断点,但是每次之后

When I call it the first time (in debug mode), it hits the breakpoint, but everytime after that it does not (which means it's not applying the YAML setting).

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

How do I disable it by profile?

推荐答案

您可以按如下所示通过配置文件派生您的cachemanager

You can derive your cachemanager by profile as given below

@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定义有条件请在此处

In case you want to have conditions on the bean definition refer here

这篇关于无法使用YAML Spring Profile禁用@Cacheable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆