Unable to disable @Cacheable with YAML Spring Profile

一个人想着一个人 提交于 2019-12-08 13:18:46

问题


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...
}

I also have the following in my application.yml:

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

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

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?


回答1:


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;
}
}

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



来源:https://stackoverflow.com/questions/46817834/unable-to-disable-cacheable-with-yaml-spring-profile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!