Add missing cache-resolver attribute

Prior to this commit, CacheResolver could not be configured through
the XML namespace (i.e. cache:annotation-driven). This is now the
case.

Issue: SPR-11490
This commit is contained in:
Stephane Nicoll
2014-05-21 08:32:46 +02:00
parent 1338d46a6e
commit 9952973e01
10 changed files with 174 additions and 17 deletions

View File

@@ -42,7 +42,7 @@ public interface CachingConfigurer {
/**
* Return the cache manager bean to use for annotation-driven cache
* management. A default {@link CacheResolver} will be initialized
* behind the scene with this cache manager. For more fine-grained
* behind the scenes with this cache manager. For more fine-grained
* management of the cache resolution, consider setting the
* {@link CacheResolver} directly.
* <p>Implementations must explicitly declare
@@ -65,8 +65,10 @@ public interface CachingConfigurer {
/**
* Return the {@link CacheResolver} bean to use to resolve regular caches for
* annotation-driven cache management. This is an alternative option to set
* the {@link CacheManager} to use.
* annotation-driven cache management. This is an alternative and more powerful
* option of specifying the {@link CacheManager} to use.
* <p>If both a {@link #cacheManager()} and {@link #cacheResolver()} are set, the
* cache manager is ignored.
* <p>Implementations must explicitly declare
* {@link org.springframework.context.annotation.Bean @Bean}, e.g.
* <pre class="code">

View File

@@ -98,9 +98,20 @@ class AnnotationDrivenCacheBeanDefinitionParser implements BeanDefinitionParser
}
}
private static void parseCacheManagerProperty(Element element, BeanDefinition def) {
def.getPropertyValues().add("cacheManager",
new RuntimeBeanReference(CacheNamespaceHandler.extractCacheManager(element)));
/**
* Parse the cache resolution strategy to use. If a 'cache-resolver' attribute
* is set, it is injected. Otherwise the 'cache-manager' is set. If {@code setBoth}
* is {@code true}, both service are actually injected.
*/
private static void parseCacheResolution(Element element, BeanDefinition def, boolean setBoth) {
String name = element.getAttribute("cache-resolver");
if (StringUtils.hasText(name)) {
def.getPropertyValues().add("cacheResolver", new RuntimeBeanReference(name.trim()));
}
if (!StringUtils.hasText(name) || setBoth) {
def.getPropertyValues().add("cacheManager",
new RuntimeBeanReference(CacheNamespaceHandler.extractCacheManager(element)));
}
}
private static BeanDefinition parseErrorHandler(Element element, BeanDefinition def) {
@@ -130,7 +141,7 @@ class AnnotationDrivenCacheBeanDefinitionParser implements BeanDefinitionParser
RootBeanDefinition interceptorDef = new RootBeanDefinition(CacheInterceptor.class);
interceptorDef.setSource(eleSource);
interceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
parseCacheManagerProperty(element, interceptorDef);
parseCacheResolution(element, interceptorDef, false);
parseErrorHandler(element, interceptorDef);
CacheNamespaceHandler.parseKeyGenerator(element, interceptorDef);
interceptorDef.getPropertyValues().add("cacheOperationSources", new RuntimeBeanReference(sourceName));
@@ -170,7 +181,7 @@ class AnnotationDrivenCacheBeanDefinitionParser implements BeanDefinitionParser
RootBeanDefinition def = new RootBeanDefinition();
def.setBeanClassName(CACHE_ASPECT_CLASS_NAME);
def.setFactoryMethodName("aspectOf");
parseCacheManagerProperty(element, def);
parseCacheResolution(element, def, false);
CacheNamespaceHandler.parseKeyGenerator(element, def);
parserContext.registerBeanComponent(new BeanComponentDefinition(def, CACHE_ASPECT_BEAN_NAME));
}
@@ -239,7 +250,9 @@ class AnnotationDrivenCacheBeanDefinitionParser implements BeanDefinitionParser
RootBeanDefinition sourceDef = new RootBeanDefinition(JCACHE_OPERATION_SOURCE_CLASS);
sourceDef.setSource(eleSource);
sourceDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
parseCacheManagerProperty(element, sourceDef);
// JSR-107 support should create an exception cache resolver with the cache manager
// and there is no way to set that exception cache resolver from the namespace
parseCacheResolution(element, sourceDef, true);
CacheNamespaceHandler.parseKeyGenerator(element, sourceDef);
return sourceDef;
}

View File

@@ -32,6 +32,9 @@ import org.springframework.cache.CacheManager;
*/
public class SimpleCacheResolver extends BaseCacheResolver {
public SimpleCacheResolver() {
}
public SimpleCacheResolver(CacheManager cacheManager) {
super(cacheManager);
}