问题
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