Skip to content

Commit

Permalink
Allow custom CacheResolver
Browse files Browse the repository at this point in the history
Previously, if a bean of type `CacheResolver` was present in the context
the whole cache auto-configuration would back off. If said
`CacheResolver` hasn't been defined via the `CachingConfigurer`
infrastructure, the application context would fail with a rather
unpleasant error message.

It can be quite common to define custom `CacheResolver` beans as the cache
annotations allow to defines custom cache resolvers per operation. This
commit makes sure that the cache auto-configuration will back-off only if
the `CacheResolver` is named `cacheResolver`.

Closes spring-projectsgh-5201
  • Loading branch information
snicoll committed Feb 29, 2016
1 parent f04b517 commit d9f4d6c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.CacheAspectSupport;
import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
Expand All @@ -62,7 +61,7 @@
@Configuration
@ConditionalOnClass(CacheManager.class)
@ConditionalOnBean(CacheAspectSupport.class)
@ConditionalOnMissingBean({ CacheManager.class, CacheResolver.class })
@ConditionalOnMissingBean(value = CacheManager.class, name = "cacheResolver")
@EnableConfigurationProperties(CacheProperties.class)
@AutoConfigureBefore(HibernateJpaAutoConfiguration.class)
@AutoConfigureAfter({ HazelcastAutoConfiguration.class, RedisAutoConfiguration.class })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,19 @@ public void cacheManagerFromSupportBackOff() {
}

@Test
public void cacheResolverBackOff() throws Exception {
load(CustomCacheResolverConfiguration.class);
public void cacheResolverFromSupportBackOff() throws Exception {
load(CustomCacheResolverFromSupportConfiguration.class);
this.thrown.expect(NoSuchBeanDefinitionException.class);
this.context.getBean(CacheManager.class);
}

@Test
public void customCacheResolverCanBeDefined() throws Exception {
load(SpecificCacheResolverConfiguration.class, "spring.cache.type=simple");
validateCacheManager(ConcurrentMapCacheManager.class);
assertThat(this.context.getBeansOfType(CacheResolver.class)).hasSize(1);
}

@Test
public void notSupportedCachingMode() {
this.thrown.expect(BeanCreationException.class);
Expand Down Expand Up @@ -833,7 +840,7 @@ public ConfigurationBuilder configurationBuilder() {
}

@Configuration
@Import({ GenericCacheConfiguration.class, RedisCacheConfiguration.class })
@EnableCaching
static class CustomCacheManagerConfiguration {

@Bean
Expand All @@ -844,7 +851,7 @@ public CacheManager cacheManager() {
}

@Configuration
@Import({ GenericCacheConfiguration.class, RedisCacheConfiguration.class })
@EnableCaching
static class CustomCacheManagerFromSupportConfiguration
extends CachingConfigurerSupport {

Expand All @@ -859,18 +866,7 @@ public CacheManager cacheManager() {

@Configuration
@EnableCaching
static class GuavaCacheBuilderConfiguration {

@Bean
CacheBuilder<Object, Object> cacheBuilder() {
return CacheBuilder.newBuilder().recordStats();
}

}

@Configuration
@Import({ GenericCacheConfiguration.class, RedisCacheConfiguration.class })
static class CustomCacheResolverConfiguration extends CachingConfigurerSupport {
static class CustomCacheResolverFromSupportConfiguration extends CachingConfigurerSupport {

@Override
@Bean
Expand All @@ -890,6 +886,28 @@ public Collection<? extends Cache> resolveCaches(

}

@Configuration
@EnableCaching
static class SpecificCacheResolverConfiguration {

@Bean
public CacheResolver myCacheResolver() {
return mock(CacheResolver.class);
}

}

@Configuration
@EnableCaching
static class GuavaCacheBuilderConfiguration {

@Bean
CacheBuilder<Object, Object> cacheBuilder() {
return CacheBuilder.newBuilder().recordStats();
}

}

@Configuration
@EnableCaching
static class CaffeineCacheBuilderConfiguration {
Expand Down
4 changes: 3 additions & 1 deletion spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3241,7 +3241,9 @@ TIP: Use the `spring-boot-starter-cache` "`Starter POM`" to quickly add required
dependencies. If you are adding dependencies manually you should note that certain
implementations are only provided by the `spring-context-support` jar.

Spring Boot tries to detect the following providers (in this order):
If you haven't defined a bean of type `CacheManager` or a `CacheResolver` named
`cacheResolver` (see `CachingConfigurer`), Spring Boot tries to detect the following
providers (in this order):

* <<boot-features-caching-provider-generic,Generic>>
* <<boot-features-caching-provider-jcache,JCache (JSR-107)>>
Expand Down

0 comments on commit d9f4d6c

Please sign in to comment.